我決定創建一個完整的工作示例,這樣你就可以理解它是如何工作的;
package {
import flash.display.Shape;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
/**
* ...
* @author Martyn
*/
public class Main extends Sprite {
public var draw:Shape = new Shape();
public function Main():void {
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}
private function init(e:Event = null):void {
removeEventListener(Event.ADDED_TO_STAGE, init);
stage.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
stage.addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
trace("init");
addChild(draw);
draw.graphics.lineStyle(3, 0x000000, 300);
}
private function onMouseDown(e:MouseEvent):void
{
stage.addEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
draw.graphics.moveTo(mouseX, mouseY);
trace("onMouseDown");
}
private function onMouseUp(e:MouseEvent):void
{
stage.removeEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
trace("onMouseUp");
}
private function onMouseMove(e:MouseEvent):void
{
draw.graphics.lineTo(mouseX, mouseY);
trace("onMouseMove");
}
}
}
希望這會有所幫助。
分享您的嘗試,我們會幫您解決問題。 – loxxy