Copy link to clipboard
Copied
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.
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
...Copy link to clipboard
Copied
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{
//
}
Copy link to clipboard
Copied
Ok, thank you. I will try this. Yes, I guess in this case it does not have to be static.
Copy link to clipboard
Copied
you're welcome.
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
correct.
so, why use any static function in A?
i only use static functions in singleton (typically data containing) classes.
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
you're welcome.
Find more inspiration, events, and resources on the new Adobe Community
Explore Now