Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does Vector map() work in actionscript 3?

I'm unable to get the map() function to work at all with a Vector class.

The result always comes back null - it should return me a new vector with the values returned by the addFive function (this example is obviously not my real usecase).

The array version works as expected.

Has anyone had any luck getting map() to work with Vector?

    public function test_vector_map():void {
        var v1:Vector.<uint> = new <uint>[1,2,3];
        trace(v1); // traces 1,2,3

        var v2:Vector.<uint> = v1.map(addFive);
        trace(v2); // traces null
    }             

    protected function addFive(item:uint, index:int, vector:Vector.<uint>):uint
    {
        return item+5;
    } 

    public function test_array_map():void {
        var v1:Array = [1,2,3];
        trace(v1); // traces 1,2,3
        var v2:Array = v1.map(addSix);
        trace(v2); // traces 7,8,9
    }             

    protected function addSix(item:uint, index:int, array:Array):uint
    {
        return item+6;
    }  
like image 377
Stray Avatar asked Feb 02 '11 14:02

Stray


2 Answers

This is an actionscript/flash player bug. It seems adobe has fixed it internally but as of Flash Player 10.1 the fix has not been released.

See: https://bugzilla.mozilla.org/show_bug.cgi?id=513095

which is the root cause of our bug found here:

https://bugzilla.mozilla.org/show_bug.cgi?id=507501

Best thing you can do for now is stick with using Arrays when Map is needed

like image 163
Ian T Avatar answered Oct 01 '22 23:10

Ian T


Weird... I am seeing your confirmations. You could always go with this solution:

var v1:Vector.<uint> = new <uint>[1,2,3];
trace(v1); // traces 1,2,3

var v2:Vector.<uint> = v1.slice();
v2.forEach( addFive );
trace(v2); // traces 6,7,8
like image 32
todd anderson Avatar answered Oct 01 '22 23:10

todd anderson