As3 removeEventListener not working

It drives me crazy. Why does removeEventListeners not work?

Class constructor

public function item(brand:String, title:String, price:Number, mp:Number, path:String, sb1:*, sb2:*):void 

sb1: * and sb2: * are object hooks.

These are designated listeners:

 _sb1.addEventListener("Changed", slideBarChanged); // Price _sb2.addEventListener("Changed", slideBarChanged); // MegaPixels 

This function is called:

 private function slideBarChanged(e:Event):void { switch(e.target.type) { case "Price": if(int(e.target.currVal) > Math.abs(this.price)) { this._active = false; _sb2.removeEventListener("Changed", slideBarChanged); } else { this._active = true; _sb2.addEventListener("Changed", slideBarChanged); } break; case "MegaPixels": if(int(e.target.currVal) > Math.abs(this.mpixels)) { this._active = false; _sb1.removeEventListener("Changed", slideBarChanged); } else { this._active = true; _sb1.addEventListener("Changed", slideBarChanged); } break; } 

Everthing works, but the listener is not deleted when the element goes _active = false; Effectively this should work as follows:

If the price is too high, then ignore the megapixels and listen only to the price. If megapixels are too high, then ignore the price and listen only to megapixels.

Violation of my brain, any help is greatly appreciated. Thanx.

+4
source share
8 answers

Try

 e.target.removeEventListener("Changed", slideBarChanged); 

Unlike

 _sb1.removeEventListener("Changed", slideBarChanged); 

or

 _sb2.removeEventListener("Changed", slideBarChanged); 

Also, in a separate note, you should analyze the constant instead of a string literal for a parameter of the type of the addEventListener () and removeEventListener () methods.

 const CHANGED:String = "changed"; _sb1.addEventListener(CHANGED, slideBarChanged); _sb1.removeEventListener(CHANGED, slideBarChanged); 
+4
source

Well, just in case, someone is hoping for an answer, that's what I did.

SIMPLIFICATION

Now the logic looks like this and it works exactly the way I want.

 private function slideBarChanged(e:Event):void { if((int(this._sb1.currVal) > Math.abs(this._price)) || (int(this._sb2.currVal)) > Math.abs(this._mpixels)) { this._active = false; } else this._active = true; } 

Thanx for all your help is much appreciated.

+1
source

You add event listeners several times. Removing a listener may not delete everything.

In the following example:

 else { this._active = true; _sb2.addEventListener("Changed", slideBarChanged); } 

You can add additional conditional code with .hasEventListener, or you can make my preferred method. Remove the event listener in the row before adding it again. This is stupid, but if you try to remove the listener before adding it to each instance, you will never duplicate it. In addition, Flash silently ignores you if you try to remove a listener that does not exist.

This issue of multiple listeners is fixed in Flex with some special Flex handling, but it still appears in Flash.

+1
source

Maybe I didnโ€™t infect all the code, but if I could guess, I would say that maybe the type declaration for your objects is the reason. eg,

 sb1:*, sb2:* 

What happens if you change them to:

 sb1:IEventDispatcher, sb2:IEventDisptacher 

Easy to try anyway.

0
source

Adobe doc show:

Removes a listener from an EventDispatcher object. If there is no matching listener registered in the EventDispatcher, calling this method has no effect.

Perhaps he does not find it.

RemoveListner Method:

 public function removeEventListener(type:String, listener:Function, useCapture:Boolean = false):void 

You might want to add useCapture when calling the method.

useCapture: Boolean (default = false) - indicates whether the listener has been registered for the capture phase or the target phase and bubbles. If the listener is registered for both the capture phase and the target and bubbling phases, two calls to removeEventListener () are required to remove both, one call using useCapture () is set to true, and the other call using useCapture () is set to false.

0
source

I am a big fan of CasaLib . There are many basic libraries that are extended. one of them is RemovableEventDispatcher

you can try the library and see if it helps

0
source

Have you tried using the willTrigger () method to determine if the listener is really active? Also, did you try running a trace there to make sure your code is actually called?

0
source

There seems to be nothing wrong with the way your listeners are assigned and removed in the code you showed. I suspect one of the following:

a) The standard user interface elements in Flash use the Event.CHANGE constant to propagate the changes, which allows the string "change", while your listeners are all assigned to "Changed". If only after you add a listener to your own "change", your event listeners will be correctly called whenever the value changes, but removeEventListener ("Changed",...) will never work. In any case, you should always use string constants instead of sequences of characters when you assign or remove listeners to avoid spelling errors.

b) Your event.target does not resolve the actual slideBar, but refers to some nested DisplayObject using the bubbling event.

c) Scene instances of your slides are not the objects referenced by _sb1 and _sb2. This can happen if you create a new instance of the slide at some point, but never update the element variables. To avoid this, you can use event.target.removeEventListener() instead of addressing _sb1 or _sb2 explicitly.

d) Something is wrong with your logic. Are you sure that removeEventListener calls are ever made? Perhaps if statements are never evaluated as true? This may occur if each side of the comparison is NaN , or if the slide values โ€‹โ€‹are not passed.

I also donโ€™t understand why you want to remove listeners at all - it seems that you are creating some kind of filtering mechanism, and although it makes sense to set this._active to false , if the element is not in the specified range, you still have to listen to the SlideBar changes to activate it again.

0
source

Source: https://habr.com/ru/post/1338945/


All Articles