我有一个容器,其子项需要注册CLICK事件。容器是可拖动的,儿童不应干扰阻力或由其触发。
我很难绕过如何实现这一目标。任何帮助表示赞赏。
如果对象已被移动,通过删除任何单击处理程序,这是我最接近的。这看起来很麻烦
box.addEventListener(MouseEvent.MOUSE_DOWN, dragit);
box.circle.addEventListener(MouseEvent.CLICK, circleclick);
function dragit(e:MouseEvent)
{
box.startDrag();
box.addEventListener(MouseEvent.MOUSE_UP, stopdrag);
}
function stopdrag(e:Event)
{
if(box.x != 0)
{
box.circle.removeEventListener(MouseEvent.CLICK, circleclick);
}
box.stopDrag();
}
function circleclick(e:MouseEvent):void
{
trace('clicked');
}
答案 0 :(得分:3)
以下内容对我来说很有用,你可以从包括孩子在内的任何地方拖动容器,我添加了一个抑制点击标记,以防止从你可能使用或不使用的孩子拖动容器时的点击,具体取决于你的应用程序
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");
}
}
}