/ Published in: ActionScript 3
This code will show you how to write Mouse Events using AS3 and also some simple Drawing API as well.
Expand |
Embed | Plain Text
package { import flash.display.Sprite; import flash.events.MouseEvent; public class ExampleApplication extends Sprite{ private var _sprite:Sprite; public function ExampleApplication(){ _sprite = new Sprite(); addChild(_sprite); _sprite.graphics.beginFill(0xFFFFFF); _sprite.graphics.drawRect(0, 0, 400, 400); _sprite.graphics.endFill(); _sprite.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown); _sprite.addEventListener(MouseEvent.MOUSE_UP, onMouseUp); } private function onMouseDown(event:MouseEvent):void{ _sprite.graphics.lineStyle(1, 0, 1); _sprite.graphics.moveTo(mouseX, mouseY); _sprite.addEventListener(MouseEvent.MOUSE_MOVE, onMouseMove); } private function onMouseUp(event:MouseEvent):void{ _sprite.removeEventListener(MouseEvent.MOUSE_MOVE, onMouseMove); } private function onMouseMove(event:MouseEvent):void{ _sprite.graphics.lineTo(mouseX, mouseY); } } }
Comments
Subscribe to comments
You need to login to post a comment.

If I remove the following block of code from your example, I can not draw. I guess I am asking how to draw in an "empty" sprite, with no specific width or height or color?
_sprite.graphics.beginFill(0xFFFFFF); _sprite.graphics.drawRect(0, 0, 400, 400); _sprite.graphics.endFill();
You have to change the event listeners from sprite to stage and it will work:
stage.addEventListener(MouseEvent.MOUSEDOWN, _onMouseDown); stage.addEventListener(MouseEvent.MOUSEUP, _onMouseUp);