如何根據創建對象的類內的父對象或舞臺來引用顯示對象的座標?限制顯示對象在AS3中的拖動座標
本質上,當我從一個自定義類創建一個新的精靈對象並將其添加到顯示列表中時,我想在自定義類中包含限制拖動座標到舞臺或舞臺的一部分的代碼。
//Frame Script
import Swatch;
var test:Sprite = new Swatch();
addChild(test);
___________________
//Custom Class
package
{
import flash.display.Sprite;
import flash.events.MouseEvent;
public class Swatch extends Sprite
{
public function Swatch()
{
init();
}
private function init():void
{
var swatchObject:Sprite = new Sprite();
swatchObject.graphics.beginFill(0x0000FF, 1);
swatchObject.graphics.drawRect(100, 100, 150, 150);
swatchObject.graphics.endFill();
swatchObject.addEventListener(MouseEvent.MOUSE_DOWN, onDrag, false, 0, true);
swatchObject.addEventListener(MouseEvent.MOUSE_UP, onDrop, false, 0, true);
this.addChild(swatchObject);
}
private function onDrag(evt:MouseEvent):void
{
evt.target.startDrag();
//how to limit it's dragability to the Stage?
}
private function onDrop(evt:MouseEvent):void
{
evt.target.stopDrag();
}
}
}
不要忘記聽Event.MOUSE_LEAVE事件(附加'onDrop'),以防用戶點擊對象並讓我們離開舞臺(雖然也許這是內置於'startDrag ()'功能已經?)。參考:http://help.adobe.com/en_US/AS3LCR/Flash_10.0/flash/display/Stage.html#event:mouseLeave – Cameron 2010-04-07 16:18:54