Have misunderstanding with addEventListener

Hello!

I have an asset that is a normal MovieClip and I want to add an event listener.

Event handler, however, will be a method of a generic class that is not extended to any of the AS3 classes.

So I have a code like this:

class Foo {}

public void Foo(asset:MovieClip) {}

asset.addEventListener (MouseEvent.CLICK, this.handle_click, false, 0, true); the use of weak references

}

private void handle_click(e:MouseEvent) {}

trace ("Click!");

}

}

It compiles well but the listener does not at all. Once I change the Foo class be extended form or Sprite, the listener magically begin to operate.

Also, when I do not use the weak reference, it also starts to work.

I don't understand why. The problem could be that active object gets garbage-collected so the event listener is destroyed, but this isn't the case, the asset continues to exist on the screen.

OK I got it. The asset was not garbage collected, but the instance of class Foo has been, higher, while it was hard to notice several levels.

To prevent garbage collection only keeping not references external for all instances, you can just make a reference within an instance itself, similar to this:

(not saying it's better or anything useful at all, just a casual employee to find who might have some implementatons)

class Foo() {}

class GC protected before you call unlock()

private var: locking function.

public void Foo() {}

This.Lock = this.unlock;

}

private function unlock() {}

This.Lock = null;

}

}

Tags: Adobe Animate

Similar Questions

Maybe you are looking for