请考虑以下代码。
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;
显然不是这种特殊情况下的解决方案。有什么想法吗?
答案 0 :(得分:1)
看起来它是ROLL_OVER的限制或设计特征,请检查以下程序我已将ROLL_OVER事件更改为MOUSE_OVER并且target和currentTarget正在给出正确的sprite:
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);
}
}
}
答案 1 :(得分: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;