Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

How to remove an event listener from a different class?

Explorer ,
Jul 29, 2018 Jul 29, 2018

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.

TOPICS
ActionScript
671
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Jul 29, 2018 Jul 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);

}

pr

...
Translate
Community Expert ,
Jul 29, 2018 Jul 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{

//

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jul 29, 2018 Jul 29, 2018

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jul 29, 2018 Jul 29, 2018

you're welcome.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jul 29, 2018 Jul 29, 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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jul 30, 2018 Jul 30, 2018

correct.

so, why use any static function in A?

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jul 31, 2018 Jul 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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jul 31, 2018 Jul 31, 2018
LATEST

you're welcome.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines