8 Replies Latest reply on May 5, 2009 2:16 AM by IlyaG

    How do I use mouse events?

    IlyaG

      I didn't thought its going to be so hard but after digging in the help library I decide to post my question here:

       

      How do I use mouse click event in action script to locate the mouse coordination.

       

      This is what I been trying to do.

       

      <?xml version="1.0" encoding="utf-8"?>

      <mx:Application

      xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"

      width="100%" height="100%" backgroundColor="#eaf1f6"

      backgroundGradientAlphas="1.0,1.0"

      creationComplete="init()"

      >

       

      <mx:Script>

      <![CDATA[

       

      import mx.effects.Move;

      import mx.controls.Alert;

       

      private function init():void

      {

           mouse=new Sprite();

           mouse.addEventListener(MouseEvent.CLICK,mouseEvents);

      }

       

      private function mouseEvents(e:MouseEvent):void

      {

           Alert.show("I am alive");

      }

       

       

      ]]>

      </mx:Script>

       

       

       

      Now where did I got wrong?

        • 1. Re: How do I use mouse events?
          _Natasha_ Level 4

          Hi,

          In your code you add eventListener to sprite, but sprite is not visible, so you never get this event.

           

          You can add

          <mx:Button label="Test click" click="mouseEvents(event);"/>

          or for your sprite

          private function init():void

          {

               mouse=new Sprite();

               mouse.addEventListener(MouseEvent.CLICK,mouseEvents);

               mouse.graphics.beginFill(0xFF0000); // set red color to see this sprite

               addChild(mouse); // add to visible list of component

          }

          • 2. Re: How do I use mouse events?
            IlyaG Level 1

            Its not working, when I am running the program it shouts with run time

            error:" Error #1034: Type Coercion failed: cannot convert

            flash.display::Sprite@607c821 to mx.core.IUIComponent."

             

            This is because of the addChild() line, so when I remove it, it should

            activate the mouseEvents(e:MouseEvent) and show me my msg

            Alert.show(e.currentTarget.toString()); but it's not as well.

             

            So I don't know what the problem once again I am sending you my code, maby

            there was some thing that I missed...

             

            <mx:Script>

            <![CDATA[

            import mx.controls.Alert;

              private var mouse:Sprite;

             

                                    private function init():void

              {

              mouse=new Sprite();

                         mouse.addEventListener(MouseEvent.CLICK,mouseEvents);

                         mouse.graphics.beginFill(0xFF0000); // set red color to see

            this sprite

                         addChild(mouse);

              }

             

              private function mouseEvents(e:MouseEvent):void

              {

              Alert.show(e.currentTarget.toString());

             

              }

             

             

             

            ]]>

            </mx:Script

            • 3. Re: How do I use mouse events?
              _Natasha_ Level 4

              yep,

              it's written in docs:

              "Note: While the child argument to the method is specified as of type DisplayObject, the argument must implement the IUIComponent interface to be added as a child of a container. All Flex components implement this interface."

              Sprite doesn't implement this interface, so it's RTE.

              So you should use UIComponent instead of Sprite.

               

              private var mouse:UIComponent;

               

                           &n bsp;          private function init():void

                {

                mouse=new UIComponent();

                           mouse.addEventListener(MouseEvent.CLICK,mouseEvents);

                           mouse.graphics.beginFill(0xFF0000); // set red color to see

              this sprite

                           addChild(mouse);

                }

               

              To locate position mouseEvent has properties: localX, localY (coordinates in local object - UIComponent at your code), stageX, stageY (global coordinates)

               

              Message was edited by: _Natasha_

              • 4. Re: How do I use mouse events?
                IlyaG Level 1

                Nop this is't working, Its not giving me the run time error but

                the mouseEvents(e:MouseEvent) is never been called.

                Some thing wrong with the click event, its doesn't getting any events at

                all.

                 

                I really hope that you will be able to help me with this, now it does look a

                bit complicated.

                • 5. Re: How do I use mouse events?
                  _Natasha_ Level 4

                  Hi,

                  ok, I'll send whole code. You must to draw something on UIComponent or set top, botton, width and height properties to see this component.

                  Or you can annd click event to application to see all click events.

                   

                   

                  <?xml version="1.0" encoding="utf-8"?>

                  <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="init()" width="100%" height="100%">

                  <mx:Script>

                  <![CDATA[

                  import mx.core.UIComponent;

                  import mx.controls.Alert;

                    private var mouse:UIComponent;

                   

                              private function init():void

                    {

                    mouse=new UIComponent();

                               mouse.addEventListener(MouseEvent.CLICK,mouseEvents);

                               mouse.graphics.beginFill(0xFF0000); // set red color to see

                       // there you must draw something! otherwise width and height will be 0, and nothing is shown

                               mouse.graphics.drawRect(10, 10, 100, 100);

                               addChild(mouse);

                    }

                   

                    private function mouseEvents(e:MouseEvent):void

                    {

                    Alert.show(e.currentTarget.toString());

                   

                    }

                   

                   

                   

                  ]]>

                  </mx:Script>

                  </mx:Application>

                  • 6. Re: How do I use mouse events?
                    ATIF FAROOQ Level 3

                    Hi,

                       if you want to monitor all mouse Clicks within your Application

                    irrespective of on what object this event was dispatched then you can add

                    global event listener on your main Application like this

                     

                    Application.application.addEventListener(MouseEvent.CLICK,mouseEvents);

                     

                    or in your Application Tag like this

                    <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"

                    click='mouseEvents(event)'

                    • 7. Re: How do I use mouse events?
                      IlyaG Level 1

                      Tnx a lot now I understand how this is working, for my aplication I choose to add the event listener to my canvas, thank you!

                      • 8. Re: How do I use mouse events?
                        IlyaG Level 1

                        tnx Natasha for your time, I found the way now