How to assign/parse JSON data from 1 as file to another? - actionscript-3

and trying to create this search form with a few fields and a search button,when search button is clicked, it goes to next page to display the results and place the results into boxes. E.g. Result 1 has(name,Location,zip), result 2(name,Location,zip) etc
What i need is, php to search and echo the data in JSON, and AS3 VO to receive the data in JSON, Split the data and parse the data to the Vector file and then put them into boxes.
I believe i am just missing 1 final step, i can get the JSON into AS3, i can trace the data. But i can't get it from the first AS(searchVO) file into the Vector AS(SEarchVector) file.
Please give me some opinion thanks.
AS3 VO(receiving the JSON)
function Asandler(event:Event):void{
trace( event.target.data );
var resultString :String = event.target.data;
// parse result string as json object
var resultObject :Object = JSON.parse( resultString );
// loop all keys in the object
for( var s:String in resultObject )
{
switch(s)
{
case 'nobed'
:'nobed', resultObject[s] ;
break;
case 'zip'
:zip=( 'zip', resultObject[s] );
break;
case 'Location'
:Location=( 'Location', resultObject[s] );
break;
case 'price'
:price=( 'price', resultObject[s] );
break;
}}}
SearchVector
public class SearchVectorTest extends MovieClip
{
public function SearchVectorTest()
{
super();
// This part is the parser, i assume i just have to fix this part and the JSON data will come through, but not sure how
var test:Vector.<searchVO1> = new Vector.<searchVO1>();
for (var i:int = 0; i < 10; i++)
{
var tests:searchVO1 = new searchVO1();
tests.nobed = nobed ;
tests.zip = zip;
tests.Location = Location[i];
tests.price = price[i];
test.push(tests);
}
// using that we can loop through every book in the entry
for (var j:int = 0; j < test.length; j++)
{
trace(test[j].nobed);
trace(test[j].zip);
trace(test[j].Location);
trace(test[j].price);
test[j].nobed;
test[j].zip;
test[j].Location;
test[j].price;
}
// loop and put the text into boxes
var currentY:int = 270;
for (var k:int = 0; k < test.length; k++)
{
var Bolder:Listing5 = new Listing5();
Bolder.x=80;
var bf:TextField = new TextField();
var bf1:TextField = new TextField();
var bf2:TextField = new TextField();
var bf3:TextField = new TextField();
bf3.width = 100;
bf.defaultTextFormat = new TextFormat("Arial", 12, 0, null, null, null, null, null, TextFormatAlign.CENTER);
bf.width = 100;
bf.autoSize = TextFieldAutoSize.CENTER;
bf1.width = 100;
bf1.autoSize = TextFieldAutoSize.CENTER;
bf2.autoSize = TextFieldAutoSize.CENTER;
bf3.autoSize = TextFieldAutoSize.CENTER;
bf3.width = 100;
bf1.y= bf.height+5;
// Pulling the textfields content out from the current bookVO
bf.text = test[k].nobed;
bf1.text = test[k].zip;
bf2.text = test[k].Location;
bf3.text = test[k].price;
bf1.x = (Bolder.height-bf.height)*.5
bf3.x = (Bolder.height-bf.height)*.5
bf.x = (Bolder.height-bf.height)*.5
bf.y = (Bolder.height-bf.height)*.15
Bolder.addChild(bf);
Bolder.addChild(bf1);
Bolder.addChild(bf2);
Bolder.addChild(bf3);
// position the object based on the accumulating variable.
Bolder.y = currentY;
//visualAssetForBook.y = (visualAssetForBook.height+15)*k;
addChild(Bolder);
currentY += Bolder.height + 35;
}
}
}
}
Just tried it and it has 2 error,
On this line
searchVOs[i] = new searchVO1(resultArray[i].nobed, resultArray[i].zip, resultArray[i].Location, resultArray[i].price ); in the searchVO file.
Error says Incorrect number of arguments. Expect no more than 0.
And 1 line below that, createBoxes(searchVOs); it has an error,
Call to a possibly undefined method createBoxes.

If you wanted to pass it as VO it's better to use the following solution:
PHP:
$result = mysqli_query($con,$sql);
// create your output array
$output = array();
// fetch your results
while( $row = mysqli_fetch_assoc($result) !== false )
{
// add result row to your output's next index
$output[] = $row;
}
// echo the json encoded object
echo json_encode( $output );
and now you can retrieve your result directly as follow:
SearchVO.as:
package
{
public class SearchVO
{
public var nobed :String;
public var zip :String;
public var location :String;
public var price :Number;
public function SearchVO( nobed:String, zip:String, location:String, price:Number )
{
this.nobed = nobed;
this.zip = zip;
this.location = location;
this.price = price;
}
}
}
ASHandler function:
// retrieve data from php call
var resultString :String = event.target.data;
// parse result string as json object and cast it to array
var resultArray :Array = JSON.parse( resultString ) as Array;
// get the length of the result set
var len:int = resultArray.length;
// create vector of SearchVO
var searchVOs:Vector.<SearchVO> = new Vector.<SearchVO>();
// loop the result array
for( var i:int = 0; i<len; ++i )
{
searchVOs[i] = new SearchVO(resultArray[i].nobed, resultArray[i].zip, resultArray[i].Location, resultArray[i].price );
}
// call a function to create your boxes
createBoxes(searchVOs);
// or maybe create your SearchVector class and pass it your search vector
var mySearchVector:SearchVector = new SearchVector(searchVOs);
createBoxes function:
private function createBoxes( test:vector.<SearchVO> ):void
{
// loop and put the text into boxes
var currentY:int = 270;
for( var k:int = 0; k < test.length; k++ )
{
var Bolder:Listing5 = new Listing5();
Bolder.x=80;
var bf:TextField = new TextField();
var bf1:TextField = new TextField();
var bf2:TextField = new TextField();
var bf3:TextField = new TextField();
bf3.width = 100;
bf.defaultTextFormat = new TextFormat("Arial", 12, 0, null, null, null, null, null, TextFormatAlign.CENTER);
bf.width = 100;
bf.autoSize = TextFieldAutoSize.CENTER;
bf1.width = 100;
bf1.autoSize = TextFieldAutoSize.CENTER;
bf2.autoSize = TextFieldAutoSize.CENTER;
bf3.autoSize = TextFieldAutoSize.CENTER;
bf3.width = 100;
bf1.y= bf.height+5;
// Pulling the textfields content out from the current bookVO
bf.text = test[k].nobed;
bf1.text = test[k].zip;
bf2.text = test[k].Location;
bf3.text = test[k].price;
bf1.x = (Bolder.height-bf.height)*.5
bf3.x = (Bolder.height-bf.height)*.5
bf.x = (Bolder.height-bf.height)*.5
bf.y = (Bolder.height-bf.height)*.15
Bolder.addChild(bf);
Bolder.addChild(bf1);
Bolder.addChild(bf2);
Bolder.addChild(bf3);
// position the object based on the accumulating variable.
Bolder.y = currentY;
//visualAssetForBook.y = (visualAssetForBook.height+15)*k;
addChild(Bolder);
currentY += Bolder.height + 35;
}
}
or SearchVector Class:
public class SearchVectorTest extends MovieClip
{
public function SearchVectorTest(test:Vector.<SearchVO>)
{
super();
// This part is the parser, i assume i just have to fix this part and the JSON data will come through, but not sure how
// REMOVE THAT !
/*
var test:Vector.<searchVO1> = new Vector.<searchVO1>();
for (var i:int = 0; i < 10; i++)
{
var tests:searchVO1 = new searchVO1();
tests.nobed = nobed ;
tests.zip = zip;
tests.Location = Location[i];
tests.price = price[i];
test.push(tests);
}
*/
// using that we can loop through every book in the entry
for (var j:int = 0; j < test.length; j++)
{
trace(test[j].nobed);
trace(test[j].zip);
trace(test[j].Location);
trace(test[j].price);
// trace if you want but calling variable just for fun is not usefull
/*
test[j].nobed;
test[j].zip;
test[j].Location;
test[j].price;
*/
}
// loop and put the text into boxes
var currentY:int = 270;
for (var k:int = 0; k < test.length; k++)
{
var Bolder:Listing5 = new Listing5();
Bolder.x=80;
var bf:TextField = new TextField();
var bf1:TextField = new TextField();
var bf2:TextField = new TextField();
var bf3:TextField = new TextField();
bf3.width = 100;
bf.defaultTextFormat = new TextFormat("Arial", 12, 0, null, null, null, null, null, TextFormatAlign.CENTER);
bf.width = 100;
bf.autoSize = TextFieldAutoSize.CENTER;
bf1.width = 100;
bf1.autoSize = TextFieldAutoSize.CENTER;
bf2.autoSize = TextFieldAutoSize.CENTER;
bf3.autoSize = TextFieldAutoSize.CENTER;
bf3.width = 100;
bf1.y= bf.height+5;
// Pulling the textfields content out from the current bookVO
bf.text = test[k].nobed;
bf1.text = test[k].zip;
bf2.text = test[k].Location;
bf3.text = test[k].price;
bf1.x = (Bolder.height-bf.height)*.5
bf3.x = (Bolder.height-bf.height)*.5
bf.x = (Bolder.height-bf.height)*.5
bf.y = (Bolder.height-bf.height)*.15
Bolder.addChild(bf);
Bolder.addChild(bf1);
Bolder.addChild(bf2);
Bolder.addChild(bf3);
// position the object based on the accumulating variable.
Bolder.y = currentY;
//visualAssetForBook.y = (visualAssetForBook.height+15)*k;
addChild(Bolder);
currentY += Bolder.height + 35;
}
}
}
In all solutions you have to pass the array (or vector) to a function, you cannot retrieve it from nowhere like you do in this part of code:
for (var i:int = 0; i < 10; i++)
{
var tests:searchVO1 = new searchVO1();
tests.nobed = nobed ; // nobed don't exists here
tests.zip = zip; // zip don't exists here
tests.Location = Location[i]; // Location don't exists here and so Location[i]
tests.price = price[i]; // pricedon't exists here and so price[i]
test.push(tests);
}
I hope this could help you
PS: try use capital letters on begin of word only for class names and use lowercase start letters for variables. It's more clear for everyone ;) (ex: MyClassName, myVarName)

Related

Images all added to last sprite, but it is already in a loop

What I am trying to do is:
1.) I parse vectors into the class. It contains simple text,
2.) In a loop it adds the text and (load then add 1 image) into a sprite.
3.) It loops, and then creates a list of boxes where each box has some text and 1 image.
The problem:
I am having a problem. The text adds into the sprite with no problem. It adds all the textfields to it, but all the images are added to the last sprite.
Thanks for your time!
My Code:
for (var k:int = 0; k < test.length; k++)
{
var Bolder:Listing8 = new Listing8();
Bolder.x=20;
if(test[k].pic1){
var loader:Loader = new Loader();
var loading111:loading1 = new loading1;
Bolder.addChild(loading111);
loader.load(new
URLRequest("http://www.rentaid.info/rent/"+test[k].pic1));
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onImageLoaded);
Bolder.addChild(loader);
}
function onImageLoaded(e:Event):void {
removeChild(loading111);
e.currentTarget.loader.content.height =50;
e.target.content.x = 130;
e.target.content.y = 15;
e.target.content.height = 70;
e.target.content.width = 140;
var bf:TextField = new TextField();
var bf1:TextField = new TextField();
var bf2:TextField = new TextField();
var bf3:TextField = new TextField();
bf3.width = 100;
bf.defaultTextFormat = new TextFormat("Arial", 12, 0, null, null, null, null, null, TextFormatAlign.CENTER);
bf.width = 100;
bf.autoSize = TextFieldAutoSize.CENTER;
bf1.width = 100;
bf1.autoSize = TextFieldAutoSize.CENTER;
bf2.autoSize = TextFieldAutoSize.CENTER;
bf3.autoSize = TextFieldAutoSize.CENTER;
bf3.width = 100;
bf1.y= bf.height+5;
// Pulling the textfields content out from the current bookVO
bf.text = test[k].nobed;
bf1.text = test[k].zip;
bf2.text = test[k].Location;
bf3.text = test[k].price;
bf.x = (Bolder.height-bf.height)*.2
Bolder.addChild(bf);
Bolder.addChild(bf1);
Bolder.addChild(bf2);
Bolder.addChild(bf3);
// position the object based on the accumulating variable.
Bolder.y = currentY;
Bolder.mouseChildren = false; // ignore children mouseEvents
Bolder.mouseEnabled = true; // enable mouse on the object - normally set to true by default
Bolder.useHandCursor = true; // add hand cursor on mouse over
Bolder.buttonMode = true;
trace ('add image with index', k);
image[k]= new Sprite();
image[k].addChild(Bolder);
image.mouseChildren = true; // ignore children mouseEvents
image.mouseEnabled = true; // enable mouse on the object - normally set to true by default
image.useHandCursor = true; // add hand cursor on mouse over
image.buttonMode = true;
image[k].addEventListener(MouseEvent.CLICK, gotoscene);
currentY += Bolder.height + 10;
}
if( test.length > 0 )
{
_contentHolder = new Sprite();
addChild(_contentHolder);
for (var j:int = 0; j < test.length; j++) {
_contentHolder.addChild(image[j]);
}
I do not recommend you to put function declaration into a function.
This is the reason of your confusion.
Your program at first executes all code(for example 10 times) EXCEPT this part:
function onImageLoaded(e:Event):void {
e.currentTarget.loader.content.height =50;
var sprite:Sprite = new Sprite;
sprite.y += sprite.height + 10;
sprite.addChild(e.target.content as Bitmap);
Bolder.addChild(sprite);
}
And then executes this part 10 times. And in this situation Bolder is always Bolder == “your_10th_Bolder”
This how events work.
Adding loader as child may help:
loader.load(new URLRequest("http://www.rentaid.info/rent/"+test[k].pic1));
Bolder.addChild(loader);
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onImageLoaded);
and in handler doing this:
function onImageLoaded(e:Event):void {
e.target.content.x = 123;
e.target.content.y = 456;
e.target.content.height = 789;
e.target.content.width = 123;
}

How to get indexof a Bitmap image?

I am having a hard time to get an index of a bitmap image. I am not sure how should i suppose to do it.
What i am trying to do is:
1.)Loop a URLRequest and load bitmap pictures.
2.)Put them in individual _contentHolder
3.)Put everything in a viewport
4.)Check the index of the image when its clicked
Thanks for your time
Code
public var _contentHolder:Sprite = new Sprite;
public var _contentHolder1:Sprite;
public var loadedArray:Array = new Array;
public var blackBox:Sprite = new Sprite();
private var somedata:Array;
protected var Holder:Listing9 = new Listing9;
public var viewport:Viewport = new Viewport();
public var scroller:TouchScroller = new TouchScroller();
var my_url:Array = somedata;
function loadImage():void
{
somedata = SearchVectorTest.lists;
for (var i:int = 5; i < somedata.length; i++)
{
if (somedata[i])
{
var loader:Loader = new Loader();
loader.load(new URLRequest("http://www.rentaid.info/rent/" + somedata[i]));
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onImageLoaded);
}
}
}
function onImageLoaded(e:Event):void
{
loadedArray.push(e.currentTarget.loader.content as Bitmap);
for (var i:int = 0; i < loadedArray.length; i++)
{
var currentY1:int = 200;
e.currentTarget.loader.content.height = 200;
e.currentTarget.loader.content.y += currentY1;
currentY1 += e.currentTarget.loader.content.height + 300;
_contentHolder.mouseChildren = false; // ignore children mouseEvents
_contentHolder.mouseEnabled = true; // enable mouse on the object - normally set to true by default
_contentHolder.useHandCursor = true; // add hand cursor on mouse over
_contentHolder.buttonMode = true;
_contentHolder.addChild(loadedArray[i]);
}
var viewport:Viewport = new Viewport();
viewport.y = 0;
viewport.addChild(_contentHolder);
var scroller:TouchScroller = new TouchScroller();
scroller.width = 300;
scroller.height = 265;
scroller.x = 10;
scroller.y = 100;
scroller.viewport = viewport;
addChild(scroller);
_contentHolder.addEventListener(MouseEvent.CLICK, gotoscene);
}
loadImage();
public function gotoscene(e:MouseEvent):void
{
BitmapArray.push(loadedArray);
var index:Number;
index = BitmapArray.indexOf(e.target);
trace(index);
trace(_contentHolder);
trace(_contentHolder.parent);
blackBox.graphics.beginFill(0x000000);
blackBox.graphics.drawRect(-1, -1, stage.width, stage.height);
blackBox.alpha = 0.7;
addChild(blackBox);
Holder.height = 300;
Holder.width = stage.width;
Holder.x = 0;
Holder.y = 100;
trace(blackBox);
trace(blackBox.parent);
addChild(Holder);
}
function gotoscene1(e:MouseEvent):void
{
removeChild(Holder);
removeChild(blackBox);
}

How do i select a specific part of an array or Vector when MouseEvent clicked?

Hi i am new to AS3 just couple of months into OOP i am not sure how do i select a specific part of an array or Vector when MouseEvent clicked.
So i parsed a list of Vector into this class called SearchVectorTest , and they are put into containers, i am trying to select the specific part of the Vector(which is text) when a specific container(box)that contain that part of the text is clicked. So that i can parse it to the next class file for further use.
At the moment i only have some idea about how to get the index of the part clicked which is below but it doesnt work.
var clickedpart:listings = Holder.target as Listing8;
var listIndex:uint = listings.indexOf(clickedpart);
trace("You clicked the part at index " + listIndex);
and i just tried this trace( bf.text+bf1.text+ bf2.text+bf3.text);
and it trace the last 4 text correctly, but not the one that i clicked.
This is the fullset of the SearchVectorTest
package com.clark
{
import flash.display.MovieClip;
import flash.text.TextField;
import flash.text.TextFieldAutoSize;
import flash.text.TextFormat;
import flash.text.TextFormatAlign;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
public class SearchVectorTest extends MovieClip
{
public var listings:Vector.<Listing8>;
public var _contentHolder: Sprite;
public function SearchVectorTest(test:Vector.<searchVO1>)
{
for (var j:int = 0; j < test.length; j++)
{
trace(test[j].nobed);
trace(test[j].zip);
trace(test[j].Location);
trace(test[j].price);
}
var james:int = test ? test.length : 0;
listings = new Vector.<Listing8>(james, true);
var currentY:int = 100;
for (var k:int = 0; k < test.length; k++)
{
var Bolder:Listing8 = new Listing8();
Bolder.x=20;
var bf:TextField = new TextField();
var bf1:TextField = new TextField();
var bf2:TextField = new TextField();
var bf3:TextField = new TextField();
bf3.width = 100;
bf.defaultTextFormat = new TextFormat("Arial", 12, 0, null, null, null, null, null, TextFormatAlign.CENTER);
bf.width = 100;
bf.autoSize = TextFieldAutoSize.CENTER;
bf1.width = 100;
bf1.autoSize = TextFieldAutoSize.CENTER;
bf2.autoSize = TextFieldAutoSize.CENTER;
bf3.autoSize = TextFieldAutoSize.CENTER;
bf3.width = 100;
bf1.y= bf.height+5;
// Pulling the textfields content out from the current bookVO
bf.text = test[k].nobed;
bf1.text = test[k].zip;
bf2.text = test[k].Location;
bf3.text = test[k].price;
bf.x = (Bolder.height-bf.height)*.2
Bolder.addChild(bf);
Bolder.addChild(bf1);
Bolder.addChild(bf2);
Bolder.addChild(bf3);
Bolder.properties = test[k].nobed;
Bolder.properties = test[k].zip;
// position the object based on the accumulating variable.
Bolder.y = currentY;
addChild(Bolder);
Bolder.mouseChildren = false; // ignore children mouseEvents
Bolder.mouseEnabled = true; // enable mouse on the object - normally set to true by default
Bolder.useHandCursor = true; // add hand cursor on mouse over
Bolder.buttonMode = true;
listings[k] = Bolder;
currentY += Bolder.height + 10;
}
if( listings.length > 0 )
{
_contentHolder = new Sprite();
addChild(_contentHolder);
for (var j:int = 0; j < listings.length; j++) {
_contentHolder.addChild(listings[j]);
}
_contentHolder.addEventListener(MouseEvent.CLICK, gotoscene);
}
function gotoscene(e: MouseEvent):void{
var clickedpart:Listing8 = Bolder.target as Listing8;
var listIndex:uint = listings.indexOf(clickedpart);
trace( bf.text+bf1.text+ bf2.text+bf3.text);
while(_contentHolder.numChildren > 0)
{
_contentHolder.removeChildAt(0);
}
while(GLOBALS.resultholder.numChildren > 0)
{
GLOBALS.resultholder.removeChildren();
}
var s5:Listingdetail= new Listingdetail ();
addChild(s5);
}
}
}
}
Thanks for your time
function gotoscene(e: MouseEvent):void{
var searchString = listings;
var index:Number;
index = searchString.indexOf(e.target);
trace(test[index].nobed+test[index].zip+test[index].Location+test[index].price);
}

stream bytearray to server using flash

I can easily send bytearray to server using these codes :
var req:URLRequest = new URLRequest(_var1);
req.contentType = 'application/octet-stream';
req.method = URLRequestMethod.POST;
req.data = DATAS;
var loader:URLLoader = new URLLoader(req);
in this data , DATAS is a bytearray , and it is ready, but I want to send byterray that is generated live and have not length in fact .
I want to stream bytearray , How can I do this ?
You need Socket connection:
var socket:Socket = new Socket();
socket.connect(host, port);
socket.addEventListener(Event.CONNECT, function(event:Event):void
{
socket.writeBytes(DATAS);
});
UPD for the test code for the comments:
var _buffer:ByteArray = new ByteArray();
_buffer.writeByte(0xFF);
trace("_buffer=",ByteArrayUtil.getHexBytesString(_buffer));
var ou:ByteArray = new ByteArray();
ou.writeBytes(_buffer , 0 ,_buffer.length);
_buffer.clear();
trace("ou=\t", ByteArrayUtil.getHexBytesString(ou));
trace("_buffer=", ByteArrayUtil.getHexBytesString(_buffer));
public static function getHexBytesString(array:ByteArray, colons:Boolean=false, position:int = 0, len:int = 0):String {
var s:String = "";
if(!array)
return s;
if(len)
{
len += position;
len = Math.min(len, array.length);
}
else
len = array.length;
for (var i:int=position;i<len;i++) {
s+=("0"+array[i].toString(16)).substr(-2,2).toUpperCase();
if (colons && i<len-1) s+= "-";
}
return s;
}
output:
_buffer= FF
ou= FF
_buffer=

How to get dynamic TextInput field values in Actionscript 3

Hi creating dynamic TextInput fields in a function. Need to get the values of those fields in another function. Can anyone throw some light on this.
for(var i:int=0;i<answers.length;i++)
{
txtbox = new spark.components.TextInput();
var lblBox:spark.components.Label = new spark.components.Label();
lblBox.id = "lbl"+i.toString();
lblBox.text = String(answersLabel.getItemAt(i) );
lblBox.width = 10
lblBox.x = xPos-15;
lblBox.y = yPos;
QuestionAnswer.addElement(lblBox);
txtbox.id = "text"+i.toString();
txtbox.x = xPos;
txtbox.y = yPos;
QuestionAnswer.addElement(txtbox);
xPos += 200;
}
var txt:TextField;
var i:uint;
var ary:Array = new Array();
function txtCreation ():void {
for( i=0;i<5;i++)
{
txt = new TextField();
txt.text = "txt"+i;
addChild(txt);
txt.x = 50 + txt.width *i;
txt.y = 20;
ary.push(txt);
}
}
txtCreation();
for(i=0;i<ary.length;i++)
{
trace("array values : " +ary[i].text);
}
Just look at the text variable for the textfield.
var textFromField : String = myInputText.text;