开发者

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:Arra开发者_JAVA百科y = v1.map(addSix);
        trace(v2); // traces 7,8,9
    }             

    protected function addSix(item:uint, index:int, array:Array):uint
    {
        return item+6;
    }  


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


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜