Skip to main content
henryl53269518
Known Participant
July 29, 2018
Answered

How to remove an event listener from a different class?

  • July 29, 2018
  • 3 replies
  • 755 views

I have 2 class of code, A and B

In class B there is a public static function addEL like this

public static function addEL (targetObject:Object):void

{

     //add click event listener on btn_B

     targetObject.btn_B.addEventListener(MouseEvent.CLICK, btnB_doSomething);

     function btnB_doSomething(event:MouseEvent):void

     {

          //do something

     }

}

This addEL function is called in class A right after I add a movieClip instance of Class B onto the stage.

What I want to do is to use removeEventListener() to remove that handler from class A by pressing a key

It works fine if I use removeEventListener()  in that addEL function by creating a click event listener in addEL

but in this way, I couldn't find a way to trigger it by pressing a key since that function only runs one time.

I'm not very familiar with the effective range of the function removeEventlistener(), what is the range limit of it or how does it work?

How exactly should I do if I want to remove the handler in a different Class, is it even possible to achieve?

I tried to find answer on all different places, but couldn't find a similar situation.

This topic has been closed for replies.
Correct answer kglad

it works like every property and method in a class: add a static function to remove the event listener.

but if you have any more questions about your code, you should explain why you're using static functions because that does not look like the appropriate use of a static function.

typically, you would use:

public function addEL(obj:InteractiveObject):void{
obj.addEventListener(MouseEvent.CLICK,f);

}

public function removeEL(obj;InteractivateObject):void{

obj.removeEventListener(MouseEvent.CLICK,f);

}

private function f(e:MouseEvent):void{

//

}

3 replies

henryl53269518
Known Participant
July 31, 2018

True, I first use "static" only for some var I need to call anytime to prevent not finding it, then it became a bad habit.

I get it now, it's a bad practice to use a static function only to do some action such as game update.

Maybe I will try not to use "static" for the caller function, thank you.

kglad
Community Expert
Community Expert
July 31, 2018

you're welcome.

henryl53269518
Known Participant
July 30, 2018

I use your method and basically the major problem is all solved.

The minor thing is if I don't use "static" for addEL removeEL and the EL triggered function in class B,

there would be an error since  the caller of those functions is also in a static function in class A.

Maybe I have a wrong concept how static function work.

So maybe static function can only call static function?

Anyway, there is no big issue as long as the original problem is solved.

kglad
Community Expert
Community Expert
July 30, 2018

correct.

so, why use any static function in A?

i only use static functions in singleton (typically data containing) classes.

kglad
Community Expert
kgladCommunity ExpertCorrect answer
Community Expert
July 29, 2018

it works like every property and method in a class: add a static function to remove the event listener.

but if you have any more questions about your code, you should explain why you're using static functions because that does not look like the appropriate use of a static function.

typically, you would use:

public function addEL(obj:InteractiveObject):void{
obj.addEventListener(MouseEvent.CLICK,f);

}

public function removeEL(obj;InteractivateObject):void{

obj.removeEventListener(MouseEvent.CLICK,f);

}

private function f(e:MouseEvent):void{

//

}

henryl53269518
Known Participant
July 30, 2018

Ok, thank you. I will try this. Yes, I guess in this case it does not have to be static.

kglad
Community Expert
Community Expert
July 30, 2018

you're welcome.