views:

37

answers:

1

Dear,

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

+2  A: 

Assuming my_btn is an instance of a button on the stage, every time you create a new instance of Pets you are adding a new MouseEvent.CLICK event handler to the same my_btn instance.

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_btn instance to exist then add the MouseEvent.CLICK handler outside of the Pets class (and only add it once). If each Pets instance needs its own button then you need to instantiate a fresh instance of my_btn for each instance of Pets (currently it looks like you are referencing the same instance of my_btn across all Pets instances).

heavilyinvolved