vincent
vincent

Reputation: 1

passing parameter with eventlistener

I have the following functions:

private function createContent(slideData:Object):void 
  {
   transitions = new Transitions();
   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?

Upvotes: 0

Views: 933

Answers (3)

Tim Joyce
Tim Joyce

Reputation: 4517

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
}

Upvotes: 0

PatrickS
PatrickS

Reputation: 9572

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);
    }
    

Upvotes: 4

loxxy
loxxy

Reputation: 13151

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.

Upvotes: 0

Related Questions