【问题标题】:Remove Event listener and move movie clip to position删除事件侦听器并将影片剪辑移动到位置
【发布时间】:2012-04-13 10:15:25
【问题描述】:

我在语法正确时遇到了一些麻烦。

我有一个影片剪辑,当它接触其他影片剪辑时,它会向数组添加声音。 我有一个停止按钮,而不是我想删除栏的事件侦听器并发送回原始位置。

我的代码是:

//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();
        }
    }
}

【问题讨论】:

  • 您遇到的一个问题是 function mouseClick3() 嵌套在另一个 function mouseClick3() 中。

标签: flash actionscript flash-cs5 event-listener


【解决方案1】:

我猜你的函数应该是这样的:

//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,因为这是函数中的变量 local。如果您删除内部 mouseClick3,代码将执行并且您的侦听器将针对正确的函数。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-03-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-04
    • 1970-01-01
    • 2022-08-03
    相关资源
    最近更新 更多