2 Replies Latest reply: Mar 18, 2010 12:53 PM by Ross Ritchey RSS

    MouseEvents in Flash CS4 (AS3)

    Me2LoveIt2 Community Member

      Hi everyone,

       

      I have some trouble with the mouse event listener. I am trying to execute code if the mouse is on a certain position in the stage, but the code should stop as soon as the mouse is pressed and then start again as soon as the mouse is released.

       

      Here is my code:

       

      stage.addEventListener(MouseEvent.MOUSE_MOVE, mousePos);
      function mousePos( e:MouseEvent ):void{
           if(mouse is pressed){//This is where i need some help I don't know how to check if the mouse is DOWN.
                //do cool stuff here
           }else if(mouseX < (stage.stageWidth / 4)){
                mDs = ((mouseX - (stage.stageWidth / 4))^2)/((stage.stageWidth / 4)/20);
           }else if(mouseX > ((stage.stageWidth / 4)* 3)){
                mDs = ((mouseX - ((stage.stageWidth / 4)* 3))^2)/(((stage.stageWidth / 4))/20);
           }else{
                mDs = 0.5;
           }     
      }
      

       

      Thank you for any help and tips!

        • 1. Re: MouseEvents in Flash CS4 (AS3)
          Rothrock Community Member

          var isPressed:Boolean=false;

           

          stage.addEventListener(MouseEvent.MOUSE_DOWN,handleMouse);

          stage.addEventListener(MouseEvent.MOUSE_UP,handleMouse);

           

          function handleMouse(e:MouseEvent):void{

          isPressed = !isPressed

          }

           

           

          function mousePos( e:MouseEvent ):void{
               if(isPressed){//This is where i need some help I don't know how to check if the mouse is DOWN.
                    //do cool stuff here
               }else if(mouseX < (stage.stageWidth / 4)){
                    mDs = ((mouseX - (stage.stageWidth / 4))^2)/((stage.stageWidth / 4)/20);
               }else if(mouseX > ((stage.stageWidth / 4)* 3)){
                    mDs = ((mouseX - ((stage.stageWidth / 4)* 3))^2)/(((stage.stageWidth / 4))/20);
               }else{
                    mDs = 0.5;
               }    
          }

          • 2. Re: MouseEvents in Flash CS4 (AS3)
            Ross Ritchey Community Member

            Actually, you can check directly via a parameter of the MouseEvent class:

             

            function mousePos( e:MouseEvent ):void{
            
                 if(e.buttonDown){
                      //do cool stuff here
                 }else if(mouseX < (stage.stageWidth / 4)){
                      mDs = ((mouseX - (stage.stageWidth / 4))^2)/((stage.stageWidth / 4)/20);
                 }else if(mouseX > ((stage.stageWidth / 4)* 3)){
                      mDs = ((mouseX - ((stage.stageWidth / 4)* 3))^2)/(((stage.stageWidth / 4))/20);
                 }else{
                      mDs = 0.5;
                 }     
            }