我有一个带有几个动画片段的容器。我在容器中添加了一个eventlistener,并在滚动时跟踪e.target.name。问题是当我滚动e.target.name是容器的名称时,当我点击e.target.name是容器内对象的目标。有谁知道如何解决这个问题?
filmsContent.StateBtnContainer.addEventListener(MouseEvent.ROLL_OVER, stateRollOverHandler);
filmsContent.StateBtnContainer.addEventListener(MouseEvent.CLICK, stateClick);
private function stateRollOverHandler(e:MouseEvent):void{
trace(e.target.name);
}
private function stateClickHandler(e:MouseEvent):void{
trace(e.target.name);
}
答案 0 :(得分:1)
查看target
对象上currentTarget
和MouseEvent
之间的差异。
在您的代码中,如果您使用currentTarget
而不是target
属性,您将获得容器对象(StateBtnContainer)
以下是一个说明差异的简单示例:
var container : Sprite = new Sprite();
container.name="container";
container.graphics.beginFill(0xff0000);
container.graphics.drawRect(0, 0, 200, 200);
container.graphics.endFill();
addChild(container);
var child : Sprite = new Sprite();
child.name="child";
child.graphics.beginFill(0x00ff00);
child.graphics.drawRect(50, 50, 100, 100);
child.graphics.endFill();
container.addChild(child);
container.addEventListener(MouseEvent.CLICK, function(e : MouseEvent) : void {
trace("click",e.target.name);
trace("click",e.currentTarget.name);
});
container.addEventListener(MouseEvent.ROLL_OVER, function(e : MouseEvent) : void {
trace("roll over",e.target.name);
trace("roll over",e.currentTarget.name);
});