1

考虑以下代码。

var sParent:Sprite = new Sprite();
var obj:Sprite = new Sprite();
var childA:Sprite = new Sprite();
var childB:Sprite = new Sprite();

sParent.addChild(obj);
obj.addChild(childA);
obj.addChild(childB);

childB.mouseChildren = false;
childB.mouseEnabled  = false;

sParent.addEventListener(MouseEvent.CLICK, itemClickHandler);
sParent.addEventListener(MouseEvent.ROLL_OVER, onHoverIn);
sParent.addEventListener(MouseEvent.ROLL_OUT, onHoverOut);

现在,我想检测“ChildA”上的事件,但我不想检测“ChildB”上的孩子

mouseChildren = false; 

在这种特殊情况下,显然不是解决方案。有任何想法吗?

4

2 回答 2

1

看起来这是 ROLL_OVER 的限制或设计特性,请检查以下程序,我已将 ROLL_OVER 事件更改为 MOUSE_OVER,并且 target 和 currentTarget 给出了正确的精灵:

package 
{
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.events.MouseEvent;

    public class Main extends Sprite 
    {

        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

            var sParent:Sprite = new Sprite();

            var obj:Sprite = new Sprite();

            var childA:Sprite = new Sprite();

            childA.graphics.beginFill(0xff0000);
            childA.graphics.drawRect(0, 0, 100, 50);
            childA.graphics.endFill();

            var childB:Sprite = new Sprite();
            childB.x = 150;
            childB.graphics.beginFill(0x00ff00);
            childB.graphics.drawRect(0, 0, 100, 50);
            childB.graphics.endFill();

            sParent.addChild(obj);
            obj.addChild(childA);
            obj.addChild(childB);

            childB.mouseChildren = false;
            childB.mouseEnabled  = false;

            sParent.mouseEnabled = false;
            obj.mouseEnabled = false;

            sParent.addEventListener(MouseEvent.CLICK, itemClickHandler);
            sParent.addEventListener(MouseEvent.MOUSE_OVER, onHoverIn);
            sParent.addEventListener(MouseEvent.MOUSE_OUT, onHoverOut);

            addChild(sParent);
        }

        private function onHoverOut(e:MouseEvent):void 
        {
            trace(e.currentTarget.name+ " "+e.target.name);
        }

        private function onHoverIn(e:MouseEvent):void 
        {
            trace(e.currentTarget.name+ " "+e.target.name);
        }

        private function itemClickHandler(e:MouseEvent):void 
        {
            trace(e.currentTarget.name+ " "+e.target.name);
        }

    }

}
于 2012-09-06T13:17:15.483 回答
1

我遇到了同样的问题,并用一些非常出乎意料的东西解决了它......

var obj:Sprite = new Sprite();
var childA:Sprite = new Sprite();
var childB:Sprite = new Sprite();


obj.addChild(childA);
obj.addChild(childB);

obj.mouseEnabled = false; // this.
childB.mouseEnabled = false;
childB.mouseChildren = false;
于 2012-09-06T13:31:15.220 回答