Posted By

mswallace on 06/28/07


Tagged

actionscript mouse events api Drawing 3


Versions (?)

Who likes this?

10 people have marked this snippet as a favorite

andrewrench
bartekk
copyleft
Wiederkehr
pixeldata
THEPWN3R
DualBrain
TrentSkunk
tspitzr
mecsekikrisztian


Mouse Events a simple drawing application using AS3


 / Published in: ActionScript 3
 

This code will show you how to write Mouse Events using AS3 and also some simple Drawing API as well.

  1. package {
  2. import flash.display.Sprite;
  3. import flash.events.MouseEvent;
  4.  
  5. public class ExampleApplication extends Sprite{
  6. private var _sprite:Sprite;
  7.  
  8. public function ExampleApplication(){
  9. _sprite = new Sprite();
  10. addChild(_sprite);
  11. _sprite.graphics.beginFill(0xFFFFFF);
  12. _sprite.graphics.drawRect(0, 0, 400, 400);
  13. _sprite.graphics.endFill();
  14.  
  15. _sprite.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
  16. _sprite.addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
  17.  
  18. }
  19. private function onMouseDown(event:MouseEvent):void{
  20. _sprite.graphics.lineStyle(1, 0, 1);
  21. _sprite.graphics.moveTo(mouseX, mouseY);
  22. _sprite.addEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
  23. }
  24. private function onMouseUp(event:MouseEvent):void{
  25. _sprite.removeEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
  26. }
  27. private function onMouseMove(event:MouseEvent):void{
  28. _sprite.graphics.lineTo(mouseX, mouseY);
  29. }
  30. }
  31. }

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: dcyadgarpff on October 24, 2007

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();

Posted By: agitated on March 30, 2009

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);

You need to login to post a comment.