Search code examples
actionscript-3mouseeventflashdevelop

Can we turn off MouseEvents?


I've been doing some testing regarding the MouseEvents. Thanks to Neal Davis, most of it was clarified for me. Now, I want to design codes such that whenever I left click, it will begin to draw circles along the mouse's x and y coordinates. When I let go of the left mouse click, it will stop drawing circles. I tried to replicate this as much as possible but this is where I eventually got stuck:

stage.addEventListener(MouseEvent.MOUSE_DOWN, mClickOn);
stage.addEventListener(MouseEvent.MOUSE_MOVE, mMove);
stage.addEventListener(MouseEvent.CLICK, mClickOff);

public var clickOn:Boolean;
public var clickOff:Boolean;

public function mClickOn(e:MouseEvent):void
{
    clickOn = e.buttonDown;
}

public function mClickOff(e:MouseEvent):void
{
    clickOff = e.buttonDown
}

public function mMove(e:MouseEvent):void
{
    if (clickOn) //keep drawing when left click
    {
        draw.create(e.localX, e.localY);
        addChild(draw);
    }
    else if (clickOff) //don't draw when you let go of left click
    {
        null;
    }
}

The problem was that MOUSE_DOWN is true when left clicked but I assumed that when we're not left clicking anymore, it'll automatically return back to false. To my testing, it stays true after the first click. As for CLICK, it's always false.

How do I make it such that these MouseEvents can be turned on and off? I want to make a pencil tool/simulator.


Solution

  • Here's the idea:

    1. When the mouse button goes down, set a flag to true, and set it to false when the mouse goes up.
    2. Check the value of that flag every application loop (either a timer event or enter frame event)
    3. Begin listening for the MOUSE_DOWN when the "draw circles" tool is selected.
    4. Stop listening for MOUSE_DOWN when the mouse is down and begin listening for MOUSE_UP
    5. When the mouse goes up, begin listening for MOUSE_DOWN again and stop listening for MOUSE_UP.
    6. then stop listening for MOUSE_DOWN when the tool is in selected. (I don't show the tool election and deselection part below. Hopefully you get the idea though. For each tool, you'll need to replicate this process)

    Do something like this:

    private function mDown(me:MouseEvent):void{
        _mDown = true;
        stage.removeEventListener(MouseEvent.MOUSE_DOWN, mDown);
        stage.addEventListener(MouseEvent.MOUSE_UP, mUp);
    }
    
    private function mUp(me:MouseEvent):void{
        _mDown = false;
        stage.removeEventListener(MouseEvent.MOUSE_UP, mUp);
        stage.addEventListener(MouseEvent.MOUSE_DOWN,mDown);
    }
    

    and then you'll need a appTick or gameLoop or timerEvent such as

    stage.addEventListener(Event.ENTER_FRAME, applicationLoop);
    
    private function applicationLoop(e:Event):void{
        if (_mDown == true){
            drawCircles();
        }
    

    and then

    private function drawCircles():void{
        // your draw logic
    }
    

    That should do it.

    NOTE if you want to draw circles the way a spray paint tool works (keeps adding more paint even when the mouse isn't moving) check the _mDown flag each frame. If you want it to only add circles when the mouse is moving then check the flag on a MOUSE_MOVE event instead.