2009年3月31日 星期二

拖拽影片 - 使用 mouseMove

使用時機:一次移動多個DisplayObject

首先監聽要移動的Clip的MouseDown事件。
當Clip的MouseDown事件被觸發後,監聽Stage的MouseUp與MouseMove事件。
當Stage的MouseMove事件被觸發後,將滑鼠的X, Y座標設給Clip的X, Y座標。
當Stage的MouseUp事件被觸發後,移除Stage的MouseUp與MouseMove監聽事件。

import flash.display.Sprite;
import flash.events.MouseEvent;

/**
* Mouse Move Drag Sample - use mouseMove event
*/

public class MouseMoveDrag extends Sprite
{
private var ball:Ball;

public function MouseMoveDrag()
{
init();
}

private function init():void
{
ball = new Ball;
ball.x = 100;
ball.y = 100;
addChild(ball);
ball.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
}

private function onMouseDown(evt:MouseEvent):void
{
stage.addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
stage.addEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
}

private function onMouseUp(evt:MouseEvent):void
{
stage.removeEventListener(MouseEvent.MOUSE_UP, onMouseUp);
stage.removeEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
}

private function onMouseMove(evt:MouseEvent):void
{
// reset the x and y point
ball.x = mouseX;
ball.y = mouseY;
}
}



參考書目:Keith Peters.《Function Actionscript 3.0 Animation》.Friends of ED

沒有留言:

張貼留言