views:

255

answers:

3

Hey all, title may be abit misleading but i didnt know the correct way to write it.

Basically, how can i do the AS3 equivalent of this php code:

return array('x' => 0, 'y' => 0);
A: 

You can do something like this

var myArray:Array = new Array({x:'0'},{y:'1'},{x:'2'});

or

var myArray:Array = new Array({x:'0',y:'1'},{a:'1',b:'2'});
Tempname
You can use objects as an associative arrays but why are you putting these objects inside an a new Array?
Alexandre Jasmin
This is done so that the array is contiguous. This way you are able to maintain the index of your array. If you do not care about the index of the array, then using an object is better.
Tempname
The weird thing about this method is that you now need to do `myArray[0]['x']`, `myArray[1]['y']`, and so on, instead of just `myArray['x']` or `myArray['y']`. Most people don't do it this way and just use a plain Object.
davr
+2  A: 
private var map:Dictionary = new Dictionary();
map["x"] = 0;
map["y"] = 0;
Vladimir
Dictionary has some good advantages over a plain Object, but has 2 main disadvantages: 1. more overhead, and 2. more code to write (can't just do `{x:0,y:0}`, can only use the syntax as shown above)
davr
A: 

The standard way of doing it is like this. The main thing to remember is that 'Object' in AS3 is almost equivalent to PHP's associative array's.

var obj:Object = {x:0, y:0};

trace(obj['x']); // like in PHP
trace(obj.x); // also valid

// AS3 version of foreach in PHP
for(var key:String in obj) {
   trace(key +" = " + obj[key]);
}
davr