Question: What is Obverse of var animals:Pets = new Pets(); ??
Script:
package {
import flash.events.MouseEvent;
public class Pets {
public function Pets() {
// constructor code
my_btn.addEventListener(MouseEvent.CLICK, onClick)
}
private function onClick(e:MouseEvent) {
trace(Start);
}
}
}
Problem:
when i call Pets class from another class ( new Pets() ); , it’s run the class and the addEventListener work fine BUT:
First Time: trace result
Start
Second Time: trace result
Start
Start
Third Time: trace result
Start
Start
Start
As well as….
Thanks a lotttt
Assuming
my_btnis an instance of a button on the stage, every time you create a new instance ofPetsyou are adding a newMouseEvent.CLICKevent handler to the samemy_btninstance.There are a couple of different ways to fix this but it depends on how you want things to work. If you only want one
my_btninstance to exist then add theMouseEvent.CLICKhandler outside of thePetsclass (and only add it once). If eachPetsinstance needs its own button then you need to instantiate a fresh instance ofmy_btnfor each instance ofPets(currently it looks like you are referencing the same instance ofmy_btnacross allPetsinstances).