類似下面的工作對我來說,你可以從任何地方,包括兒童拖動容器,我添加了一個抑制點擊標誌,以防止點擊如果容器是從一個孩子你可能拖累根據您的應用使用或不使用。
package
{
public class Main extends Sprite
{
private var container:Sprite = new Sprite();
private var suppressClick:Boolean = false;
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);
// entry point
container.graphics.beginFill(0xff0000, 1);
container.graphics.drawRect(0, 0, 500, 500);
container.graphics.endFill();
addChild(container);
container.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
var child:Sprite = new Sprite();
child.graphics.beginFill(0x00ff00, 1);
child.graphics.drawRect(0, 0, 50, 50);
child.graphics.endFill();
child.addEventListener(MouseEvent.CLICK, onChildClick);
child.x = 100;
child.y = 100;
container.addChild(child);
}
private function onMouseDown(e:MouseEvent):void
{
suppressClick = false;
stage.addEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
stage.addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
container.startDrag();
}
private function onMouseMove(e:MouseEvent):void
{
suppressClick = true;
}
private function onMouseUp(e:MouseEvent):void
{
stage.removeEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
stage.removeEventListener(MouseEvent.MOUSE_UP, onMouseUp);
container.stopDrag();
}
private function onChildClick(e:MouseEvent):void
{
if(!suppressClick)
trace("child clicked");
}
}
}
您可以根據您已經嘗試過的方式發佈您的代碼或僞代碼嗎? – Jordan 2013-04-26 02:26:11
CLICK是MOUSE_DOWN + MOUSE_UP。但是,當您註冊MOUSE_DOWN或MOUSE_UP時,您無法再獲取CLICK。如果您在MOUSE_UP之前有MOUSE_MOVE大於某個距離(您必須根據您的項目決定什麼),則會拖動。 – 2013-04-26 02:31:16