开发者

How to pass the ... rest argument array to another function as an object property in flash actionscript 3?

I have a function that uses the ... rest argument as follows:

public function dispatchActions(... actions):void
    {
        var params:Object = new Object();       
        params.actions = actions; // I also tried casting ie. = (actions as Array)

        this.target.dispatchEvent(new ActionEvent(ActionEvent.dispatchActions, params));
    }

I call it using myActionObject.dispatchActions("all");

The problem is that when a listener receives the dispatched params object and try to access the actions开发者_开发问答 array, it does nothing. If I try to trace evt.params.actions it traces blank and if I try to trace evt.params.actions[0] it traces undefined.

ex:

    function actionsListener(evt:ActionEvent):void
    {
        var actions:Array = (evt.params.actions) as Array; // I also tried without casting

        trace("actions", actions, "actions 0", actions[0], "action 1", actions[1]);
    }

This is the output: actions actions 0 undefined actions 1 undefined

What is wrong here? I don't understand why I can't pass the ... rest argument through an object as an event parameter. How can I make this work?

Thank You.


It looks like your params object isn't getting sent with the event properly... I take it ActionEvent is your own custom event - have you checked that:

1) you are passing the params object in to the event constructor properly?

eg:

class ActionEvent extends Event{
    public var params:Object;
    public function ActionEvent(type:String, params:Object){
        super(type);
        this.params = params;
    }
}

2) if you are redispatching the event, have you defined a clone function to make sure your params gets cloned as well?

 override public function clone():Event{
    return new ActionEvent(type,params);
}


This works for me:

sample("as", "asd");
private function sample(... actions):void
{
    var o:Object = {};
    o.array = actions;
    sample1(o);
}
private function sample1(o:Object):void
{
    trace(o.array[1]);//traces asd
}


Or you can just do this:

sample("as", "asd");
private function sample(... actions):void
{
    sample1(actions);
}
private function sample1(o:Array):void
{
    trace(o[1]);//traces asd
}

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜