3 Replies Latest reply on Aug 6, 2013 11:05 AM by kglad

    Understanding Mouse events

    Charine -FB buggoop

      I want to build a word game where a player clicks on a letter tile to copy a word on screen. I want to know how this mouse event will work.

       

      I have an array of letter tiles:

       

                private var letterTiles:Array = [a,b,c,.....etc]

       

      Each letter has it's own class file like this:

      package

      {

        import flash.display.MovieClip;

        import flash.text.*;

         

          public class A extends MovieClip

          {

              public var tileLetter:String = "a";

              public function A()

              {

                // constructor code

               }

           }

      }

       

      they are shuffled and placed on screen. The next function is to see which letter was clicked on. I have

       

       

      public function initLetterWasClickedOn():void

                      {

                          for(var i:int=0;i<letterTiles.length;i++)

                          {

                              letterTiles[i].addEventListener(MouseEvent.MOUSE_DOWN, onClickLetter);

                          }

                          function onClickLetter(e:MouseEvent):void

                          {

                                  trace(e.currentTarget.name);

                              }

       

      In my output panel, when I click on a letter I get: element 26, element 1 etc.

       

      Thing is, I never named anything element # whatever. These elemant numbers do not correspond to the position of the letter in the array either. I see that letter tile 's' for example is alway element 26. Where do these names come from?

       

      More importantly - can I just go ahead and write code to compare letters:

       

      if (currentTarget.name == activeLetterInWord)

      {do these;}

      else{do these;}

       

      Thanks

      Charine

        • 1. Re: Understanding Mouse events
          kglad Adobe Community Professional & MVP

          assign names:

           

          public function initLetterWasClickedOn():void

                          {

                              for(var i:int=0;i<letterTiles.length;i++)

                              {

                                  letterTiles[i].addEventListener(MouseEvent.MOUSE_DOWN, onClickLetter);

          letterTiles[i].name=i.toString();

                              }

                          }


          function onClickLetter(e:MouseEvent):void

           

          {

          trace(e.currentTarget.name);

          //letterTiles[Number(e.currentTarget.name)] is the clicked array element

          }

           

           

          or, if those array elements are movieclips, you can just assign their index to be a movieclip property:

           

          public function initLetterWasClickedOn():void

           

                          {

           

                              for(var i:int=0;i<letterTiles.length;i++)

           

                              {

           

                                  letterTiles[i].addEventListener(MouseEvent.MOUSE_DOWN, onClickLetter);

          letterTiles[i].ivar=i;

                              }

          }

           

                              function onClickLetter(e:MouseEvent):void

           

                              {

           

                                      trace(MovieClip(e.currentTarget).ivar);

           

                                  }

          1 person found this helpful
          • 2. Re: Understanding Mouse events
            Charine -FB buggoop Level 1

            Hi kglad,

            They are indeed movieclips. I did as you suggested. See code below, but altough my swf file runs, I get the following error code on every click:

             

            ReferenceError: Error #1056: Cannot create property ivar on A.

                at LetterArray/initLetterWasClickedOn()

                at LetterArray/gameLoop()

                at BugGoopFSGame/gameLoop()

                at flash.utils::Timer/_timerDispatch()

                at flash.utils::Timer/tick()

             

            Here is the code with changes as you suggested:

             

            public function initLetterWasClickedOn():void

                            {

                                for(var i:int=0;i<letterTiles.length;i++)

                                {

                                    letterTiles[i].addEventListener(MouseEvent.MOUSE_DOWN, onClickLetter);

                                    letterTiles[i].ivar=i;

                                }

                                function onClickLetter(e:MouseEvent):void

                                {

                                        trace(e.currentTarget.ivar);

                                    }

            • 3. Re: Understanding Mouse events
              kglad Adobe Community Professional & MVP

              if A extends MovieClip, use:

               

              public function initLetterWasClickedOn():void

                              {

                                  for(var i:int=0;i<letterTiles.length;i++)

                                  {

                                      letterTiles[i].addEventListener(MouseEvent.MOUSE_DOWN, onClickLetter);

                                      MovieClip(letterTiles[i]).ivar=i;

                                  }

                                  function onClickLetter(e:MouseEvent):void

                                  {

                                          trace(MovieClip(e.currentTarget).ivar);

                                      }