views:

277

answers:

3

Hi,

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.

A: 

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
}
Amarghosh
A: 
bhups
+1  A: 

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);
}
Reuben
+1. clone() was a big gotcha for me in learning AS3. Nice answer.
Typeoneerror
Well, it could very well have been that, so I still chose this answer, but actually, it was my fault. A silly problem, I was calling dispatchActions from somewhere else, and passing it no parameters, so all this time I was tracing the wrong one.
didibus