5 Replies Latest reply on May 28, 2009 3:42 AM by *Prashant Shelke*

    problem with adding click event to image - help...

    IlyaG Level 1

      I don't know where I got wrong, try to see if you can find any error in the next code

       

      Class Unit extends Image

      {

      }

       

       

      private var _team2:Array;

       

      public function addTeam2(...units):void //addTeam2 gets 1 Unit      parameter
      {
           for (var i:int=0;i<units.length;i++)

           {

                _team2.push(units[i]);

                _team2[i].addEventListener(MouseEvent.CLICK,selectFromTeam2);

           }

      }   

       

       

      private function selectFromTeam2(e:MouseEvent):void

      {

          Alert.show(_team2[i].toString()+","+e.target.toString());

      }

       

      This is the message that I am getting from the alert:

      Main0.mainCanvas.Canvas59.Unit43 , Main0.mainCanvas.Canvas59.Unit43.FlexLoader44

       

      What I am really trying to do is just to compare between _team2 and my target, but I found that they are different types and I just don't know why, do you have any explanation for this?

        • 1. Re: problem with adding click event to image - help...
          *Prashant Shelke* Level 4

          Hi,


          I think you have to use event.currentTarget instead of just target.


          Thanks.

          • 2. Re: problem with adding click event to image - help...
            *Prashant Shelke* Level 4

            One more thing I woluld like to suggest, please see following code:


            public function addTeam2(...units):void //addTeam2 gets 1 Unit      parameter
            {
                 for (var i:int=0;i<units.length;i++)

                 {

                     units[i].name = "" + i;  /** we are setting here name because id should be identifier */

                      _team2,name.name = "" + i;

                      _team2.push(units[i]);

                      _team2[i].addEventListener(MouseEvent.CLICK,selectFromTeam2);

                 }


                /** & while comparing use (event.currentTarget.name == _team2[i].name)

                 **/

            }   


            Thanks.

            1 person found this helpful
            • 3. Re: problem with adding click event to image - help...
              IlyaG Level 1

              tnx, you just don't understand how much time I wasted for this.

              lol

              • 4. Re: problem with adding click event to image - help...
                ATIF FAROOQ Level 3

                hi following paragraph may help you

                 

                When you handle a mouse event such as MouseEvent.CLICK by writing a listener  on some component, the event.target property does not  necessarily refer to that component; it is often a subcomponent, such as the  Button control's UITextField, that defines the label.

                When Flash Player or Adobe® AIR™ dispatches an event, it dispatches the event  from the frontmost object under the mouse. Because children are in front of  parents, that means the player or AIR might dispatch the event from an internal  subcomponent, such as the UITextField of a Button.

                The event.target property is set to the object that  dispatched the event (in this case, UITextField), not the object that is being  listened to (in most cases, you have a Button control listen for a click event).

                MouseEvent events bubble up the parent chain, and can be handled on any  ancestor. As the event bubbles, the value of the event.target property stays the same (UITextField), but the  value of the event.currentTarget property is set at  each level to be the ancestor that is handling the event. Eventually, the currentTarget will be Button, at which time the Button  control's event listener will handle the event. For this reason, you should use  the event.currentTarget property rather than the event.target property; for example:

                <mx:Button label="OK" click="trace(event.currentTarget.label)"/>

                In this case, in the Button event's click event listener, the event.currentTarget property always refers to the Button,  while event.target might be either the Button or its  UITextField, depending on where on the Button control the user clicked.

                1 person found this helpful
                • 5. Re: problem with adding click event to image - help...
                  *Prashant Shelke* Level 4

                  Its happens frequently Keep knowledge shared .. it will grow... cheers