开发者

passing parameter with eventlistener

I have the following functions:

private function createContent(slideData:Object):void 
  {
   transitions = new Transitions();
开发者_JS百科   if (slide){
   transitions.applyTransition(slide);
   transitions.addEventListener(Transitions.TRANSITION_COMPLETE, completeHandler);

   }
   slide  = new Slide(slideData);
    addChild(slide);
    transitions.applyTransition(slide);
  }
  private function completeHandler(e:Event):void{
   removeChild(slide);
  }

I dispatch an event in the first function and when it comes to the completehandler i would like to delete the slide from the first function but it isnt recognized. How can i pass the slide with the eventlistener so i can remove it in the completeHandler?(i have several instances from slide so i have to pass it through to have the right instance). Anyone who can help me?


Here are a couple of ways to pass the slide to the event listener.

  • 1/ As a property of the event

    //Assuming that:
    // 1/ you create a custom Event class that takes two parameters
    //    type: String
    //    slide:Slide
    // 2/ that you have assigned the slide object to a variable in the
    // applyTransition method , which you can then assign to the event
    transitions.dispatchEvent( new TransitionEvent( 
                                Transitions.TRANSITION_COMPLETE , slide ) );
    
  • 2/ As a property of the dispatcher

    //Assuming that:
    // you assign the slide object to a variable in the
    // applyTransition method
    private function completeHandler(e:Event):void{
      var target:Transitions = event.currentTarget as Transitions;
      removeChild(target.slide);
    }
    


You can use the name property of the slide if you wish.

(Though you have not described how & where slide is actually declared - sprite, mc, etc)

Using name property :

Set slide as slide.name = "instanceName" (In your first function)

Get slide as getChildByName("instanceName") (In your second function)


Alternatively you can also:

  • Set the slide as class member, accessible by all the function of the class.
  • Add reference of every slide to an array available as class member to all its functions.


If the variable is not dynamic, you could probably use an anonymous function to pass the variable.

transitions.addEventListener(Transitions.TRANSITION_COMPLETE, function (evt:Event) {
    completeHandler(evt, variable1, variable2);
});

function completeHandler(evt, catch1, catch2) {
    //do stuff
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜