Page 1 of 1

How to turn an ISFSArray into a regular as3 Array?

Posted: 29 Jul 2014, 14:58
by Linck5
I'm just testing sending arrays to to the extension, and making the extension process these arrays using a database, an then sending it back. My test is that I have an Array with 2 float values, and I want the DB to sum each of this values with the values it has on a table, and return the result to my client.

My question is that the Array comes as an ISFSArray, and I want to turn it into a regular as3 Array class. The SFSArray class has the method "toArray()", but the ISFS doesn't. Then what I'm doing is make a for loop with this ISFSArray and pushing all elements into a new as3 new Array I created. And I want to know if this approach is right, and I guess it probably isn't.

Have a look at the code:


I send the request when the user presses spacebar;

Code: Select all

		
		override public function update():void {
			if(Input.released(Key.SPACE)){
				
				var myArray:ISFSArray = new SFSArray();
				myArray.addFloat(12.5);
				myArray.addFloat(-0.2);
				
				var params:ISFSObject = new SFSObject();
				params.putSFSArray("arr", myArray);
				
				sfs.send(new ExtensionRequest("sumWithDB", params));
				
				showOnScreen("sumWithDB extension request sent");
			}
		}
		

Then this is where I handle the extension response and show on screen:

Code: Select all

		
		protected function onExtensionResponse(event:SFSEvent):void
		{
			showOnScreen("got an extension response for the command: " + event.params.cmd);
			if (event.params.cmd == "sumWithDB")
			{
				var responseParams:ISFSObject = event.params.params as SFSObject;
				
				var responseArray:ISFSArray = responseParams.getSFSArray("sums");
				
				var regularArray:Array = new Array();
				for each(var num:Number in responseArray){
					regularArray.push(num);
				}
				
				showOnScreen("The sums are: " + regularArray);
			}
		}
		

Re: How to turn an ISFSArray into a regular as3 Array?

Posted: 30 Jul 2014, 07:26
by Lapo
Hi,
My question is that the Array comes as an ISFSArray, and I want to turn it into a regular as3 Array class. The SFSArray class has the method "toArray()", but the ISFS doesn't.
You can simply cast the ISFSArray to ---> SFSArray

Code: Select all

var regularArray:Array = ((SFSArray) myISFSArray).toArray()
Or, from your code:

Code: Select all

var responseArray:SFSArray = responseParams.getSFSArray("sums") as SFSArray;
Cheers

Re: How to turn an ISFSArray into a regular as3 Array?

Posted: 30 Jul 2014, 13:41
by Linck5
Great! That's much easier. Thanks a lot for the help.