Flash 删除事件侦听器并将电影剪辑移动到位置
我在语法上有点困难 我有一个电影剪辑,当它接触到其他电影剪辑时,会向阵列添加声音。 我有一个停止按钮,我想删除该条的事件侦听器并发送回原始位置 我的代码是:Flash 删除事件侦听器并将电影剪辑移动到位置,flash,actionscript,flash-cs5,event-listener,Flash,Actionscript,Flash Cs5,Event Listener,我在语法上有点困难 我有一个电影剪辑,当它接触到其他电影剪辑时,会向阵列添加声音。 我有一个停止按钮,我想删除该条的事件侦听器并发送回原始位置 我的代码是: //event listener for the start button playy.addEventListener(MouseEvent.CLICK, mouseClick2); function mouseClick2(event:MouseEvent):void { bar.addEventListener(Event.
//event listener for the start button
playy.addEventListener(MouseEvent.CLICK, mouseClick2);
function mouseClick2(event:MouseEvent):void
{
bar.addEventListener(Event.ENTER_FRAME, onEnter);
}
//Add event listener for the stop button
stopp.addEventListener(MouseEvent.CLICK, mouseClick3);
//when clicked remove listener send back to position
function mouseClick3(event:MouseEvent):void
{
bar.removeEventListener(MouseEvent.CLICK, mouseClick3);
function mouseClick3(evt:Event):void
{
if(bar.x > 780)
{
bar.x = 215;
}
}
}
function onEnter(evt:Event):void
{
bar.x += 1;
if(bar.x > 780)
{
bar.x = 215;
}
for(var i:int=0; i<blocks.length;i++)
{
if (bar.hitTestObject(blocks[i]))
{
blocks[i].start();
}
else
{
blocks[i].stopSound();
}
}
}
//启动按钮的事件侦听器
play.addEventListener(MouseEvent.CLICK,mouseClick2);
函数mouseClick2(事件:MouseEvent):无效
{
bar.addEventListener(Event.ENTER_FRAME,onEnter);
}
//为“停止”按钮添加事件侦听器
stop.addEventListener(MouseEvent.CLICK,mouseClick3);
//单击“删除侦听器”“发送回位置”时
函数mouseClick3(事件:MouseEvent):无效
{
bar.removeEventListener(MouseEvent.CLICK,mouseClick3);
函数mouseClick3(evt:事件):无效
{
如果(bar.x>780)
{
bar.x=215;
}
}
}
函数onEnter(evt:事件):无效
{
bar.x+=1;
如果(bar.x>780)
{
bar.x=215;
}
对于(var i:int=0;i我猜您的函数应该是:
//when clicked remove listener send back to position
function mouseClick3(event:MouseEvent):void
{
bar.removeEventListener(MouseEvent.CLICK, mouseClick3);
if(bar.x > 780)
{
bar.x = 215;
}
}
问题是你有两个“mouseClick3”函数。内部mouseClick3从未实际执行,removeEventListener将以从未执行的mouseClick3为目标,因为这是函数中的局部变量。如果删除内部mouseClick3,代码将执行,侦听器将以正确的函数为目标。您遇到的一个问题是有趣操作mouseClick3()
嵌套在另一个函数mouseClick3()
中。