删除eventListener不在按钮上工作AS3 - 闪光灯
问题描述:
我想删除按钮上的事件按钮,所以当按下按钮时,动画完成,然后再次按下按钮。但基于我的代码下面你可以多次按下按钮,只要你喜欢:删除eventListener不在按钮上工作AS3 - 闪光灯
var LeftButt:MovieClip = new left_button();
var RightButt:MovieClip = new right_button();
var topClip:Sprite = new Sprite();
addChild(topClip);
LeftButt.addEventListener(MouseEvent.MOUSE_UP, function(e){moveItems(e, "left");});
RightButt.addEventListener(MouseEvent.MOUSE_UP, function(e){moveItems(e, "right");});
function clothingApp(event:MouseEvent):void{
topClip.addChild(RightButt);
topClip.addChild(LeftButt);
}
function moveItems(event:MouseEvent, SlideDirection:String):void{
LeftButt.removeEventListener(MouseEvent.MOUSE_UP, function(e){moveItems(e, "left");});
RightButt.removeEventListener(MouseEvent.MOUSE_UP, function(e){moveItems(e, "right");});
trace(SlideDirection);
}
因此从技术上讲,因为我从来没有成立了事件监听再此代码应只运行一次。但是,您可以多次按下按钮,只要你喜欢。
答
如果你想删除事件监听器,你不能使用匿名函数来添加它们。
创建一个与匿名函数具有相同功能的包装函数,并且您会没事的。
function moveLeft(event:MouseEvent):void
{
moveItems(event, "left");
}
function moveRight(event:MouseEvent):void
{
moveItems(event, "right");
}
LeftButt.addEventListener(MouseEvent.MOUSE_UP, moveLeft);
RightButt.addEventListener(MouseEvent.MOUSE_UP, moveRight);
LeftButt.removeEventListener(MouseEvent.MOUSE_UP, moveLeft);
RightButt.removeEventListener(MouseEvent.MOUSE_UP, moveRight);
所以我把我的addeventlistener移到了我的函数外面,remove仍然在里面。我仍然可以按我想要的次数按下它。 – Denoteone 2012-02-28 17:11:21
@Denoteone,你不应该将addEventLIstener移出函数,你需要将函数移出addEventListener。 – 2012-02-28 17:26:21
AH-HA(灯泡)我明白你现在在说什么。将测试并确认它的工作原理。谢谢+1 – Denoteone 2012-02-28 17:39:27