page 12 MouseEvent


//生成的_sprite里进行鼠标绘画
package
{
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	
	//设置舞台的尺寸,帧频
	//[SWF(width = "500", height = "400", backgroundColor = "0xd4ded4", frameRate = "24")]
	
	public class Main extends Sprite
	{
		private var _sprite:Sprite;
		
		public function Main()
		{
			_sprite = new Sprite();
			addChild(_sprite);
			_sprite.graphics.beginFill(0xc7edcc);
			_sprite.graphics.drawRect(50, 50, 450, 450);
			_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);
		}
	}
}

你可能感兴趣的:(【读书笔记】AS3,CookBook,function,import,class,up)