Last time we saw the problem with sending ActionScipt 3 Associative Array. Now we let us see where associative arrays can help.
Lets Make an Array with offset index
in PHP:
$arr =array(5=>5);
in ActionScript:
var arr:Array= [];
arr[5]=5;
Sending these arrays either way they go with the missing indices filled with null, so the PHP array comes to flash as
arr=[null, null, null, null, null, 5];
flash array goes to PHP as
$arr=array(NULL, NULL, NULL, NULL, NULL, 5);
This happens in AS1 and AS2 remoting as well.
For an offset of 5 this may be fine, but think about 1000 and above. Solution to this problem is simple, we just need to convert this to associative array by defining one string based key, then the unwanted null values will be gone.
posted by Arul
| link
|<prev. | ^top
| next> |
comments [1]
Monday, July 17, 2006
Code.Flash 9: Webcam Motion Detection using AS3 - Source Released.
I rewrote the webcam motion detection example in Actionscript3 using Flash 9 Alpha. You can see the demo here.
I went through that code and completely rewrote it in ActionScript 3. Here is the demo. This one of course requires a webcam and flash 9 player to play around.
Examples.Flash 9: Understanding AMF0 and AS3 Array.
You might have already read about Array oddity in AMFPHP/Flex 2. AS3 Associative Arrays, when sent through AMF0 they include 'length' propery. But numeric index based Arrays are fine.
I've created the following AMFPHP Service to showcase this issue. It is a simple service with a remote method that converts any given object to string and returns it.
<?class AS3ArrayTest{function AS3ArrayTest(){
@include_once("AS3ArrayTest.methodTable.php");
}/**Converts the given data to String and returns it
*@param Data Any, Data that needs to be converted
*@access remote
*@returns String
*/function convertToString($data){$str = "PHP dataType: ".gettype($data)."\n";
$str .= print_r($data, true);
return$str;
}}?>
Then I've created a Flash 9 AS3 Fla to send different combinations of data to this service. here is the script that does it all.
importflash.net.Responder;
var conn : RemotingConnection = new RemotingConnection("http://localhost/amfphp/gateway.php");
Bug.AMFPHP: Service Browser does not handle method table 'arguments' metadata defined as key value pairs.
While defining the method table for my ObjectStore service I noticed it.
Those who use the JavaDoc style comments to generate the method table may never notice this issue.
Arguments metadata can be defined as an array of strings (as created by javadoc comments) or array of array which contains argument name as the key and settings as the value. More on this at amfphp.org
ServiceBroswer class always expects the arguments data as numeric index based array. If you supply key value pair it renders it as 'Array' instead of the actual argument name. Also it does not remember the arguments submitted last time.
Following picture shows the issue
I did the following modifications to '_printMethod' function in ServiceBrowser.php under the 'amfphp/browser' folder to make it work :)
What is ObjectStore?
Object Store is a service for creating, managing and accessing objects on the server. It is a light weight alternative to keeping XML data on the server.
How do we compare ObjectStore with RemoteSharedObject?
ObjectStore is similar to RemoteSharedObject in terms of storing data on the server, but ObjectStore does not poll (automatically refresh) data and does not fire events on change, although this can be achieved by writing your own Actionscript.
Do we need AMFPHP to use ObjectStore?
AMFPHP is needed only when you need to send and receive the data from flash through remoting, All the data access api is available to PHP.
What are the data types supported by ObjectStore?
following data types are supported
PHP
ActionScript
boolean
Boolean
integer
Number
float/double
Number
string
String
NULL
null
array
(index based)
Array
associate array
(key based)
Object
How do we access data in ObjectStore?
you can access object using a combination of file path and object path, say for example "myfolder/subfolder/myobject.childobject.property" ('/' is used to indicate the folders and '.' is used to indicate the objects) here "myfolder/subfolder/myobject" is the file path and "childobject.property" is the object path.
I'm planning to create a new object, which one should I prefer to use file path or object path?
consider file paths like the name spaces in as2 / package in as3. Use them to avoid name clashes and to organize your objects wisely. object path is the actual object structure which can be compared to properties of the classes in Actionscript. Lengthy object path is going to result in bigger object files on the server, so try to keep it as file path as whenever possible.
Where can I get ObjectStore?
You will be able to download ObjectStore from my blog soon, keep watching this blog for more updates :)
How much it costs?
It will be free for both noncommercial and commercial use.
Examples.Flash 9: How to load data from a remote URL?
I found that the DataLoadingTest class given in Adobe Labs ActionScript 3 Resources site was buggy and did not work. I've corrected the file myself and made it work. Here is the working code :)
Examples.Flash 9: Porting XMLHighlighter class to ActionScript 3.
XMLHighlighter generates color highlighted pretty printed HTML code for the given XML document, I have ported it from ActionScript 2 to ActionScript 3.
Here is what I learned during the process
XML object in ActionScript 3 is different (It is ECMAScript for XML (E4X)).
For backward compatibility we have XMLDocument class which is equalent to XML in Actionscript 3.
In ActionScript 3 we need to keep the class inside Package container.
I replaced all "XML" to "XMLDocument" from the source, Moved my XMLHighlighter class inside blank Package, renamed it as XMLDocumentHighlighter. That's all.
You can download the file from here. I will be writing a XMLHighlighter class which takes advantage of the new E4X XML class.
posted by Arul
| link
|<prev. | ^top
| next> |
add comment
Monday, July 03, 2006
Code.PHP: Removing Byte Order Mark (BOM) from the given string.
Since PHP does not recognize and ignore BOM in the loaded files, I wrote the following script to find and remove the BOM from the loaded string.