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.
Here's the idea:
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.