Concat multi-dimensional Arrays in AS3

How many times have you needed to concat multiple arrays together in AS3? Yet alone several multi-dimensional arrays?

Array.concat(... args);

Requires comma separated values, so it won't work with a multi-dimensional array in the forms of [[1,2],[1,3],[1,4]]

The trick is to use:

Array.forEach(callback:Function, thisObject:* = null):void

With a function that adds a incrementing zero base position value with every call to the a new array. This script below shows a simple example:

// define your arrays
var a1_array:Array = [[1,2],[1,3],[1,4]];
var a2_array:Array = [[1,5],[1,6]];
var temp_array:Array = [];
//
var i:int = 0;
a1_array.forEach(addTo);
a2_array.forEach(addTo);
//
function addTo(element:Array, index:int, arr:Array):void{
     this.temp_array[this.i++] = element as Array;
}
//
trace(temp_array);// returns a string "1,2,1,3,1,4,1,5,1,6"
trace(temp_array[2]);// returns a string "1,4"

Note: You will need to be careful with the scoping of the 'i' and 'temp_array' inside the function. If this was in a class also be wary of reuse of the function and the i not being reset to 0 each time if you are intending on comparing new arrays each time.

Word up to knowledge.

Elliot

Flash and crossdomain.xml policies

@rdougan Firstly does amazon s3 have an open crossdomain.xml setup?

As a default as3 looks at the root html directory of the domain that the .swf that is trying to connect with the content.

In this situation you need to setup the security and then load once the crossdomain.xml file from amazon:

Note: I am not sure of the correct address for your situation so I am just assuming www.amazon.com is where the policy file lives!


flash.system.Security.allowDomain("http://www.amazon.com");
flash.system.Security.allowInsecureDomain("http://www.amazon.com");
/*
       before you load your content from amazon
*/
Security.loadPolicyFile("http://www.amazon.com/crossdomain.xml");
// now load your content/data


the Security.loadPolicyFile is interesting as it halts at that point before proceeding as it has its own event listeners built in (very proceedural :)  ).

I am pretty sure that once it has loaded the file once you don't need to call the file again. The other part to consider whilst implementing this in other situations is that the crossdomain.xml needs to be at the document or html root not the file system root for that domain.

I hope this helped?