boolean没有正确停止鼠标事件as3

时间:2014-11-28 18:38:35

标签: actionscript-3 boolean mouseevent

=))我有这个简单的问题,我希望我的布尔值在用户找到正确的组合后变为假(因此在找到正确的组合后,进一步点击卡片不会继续使用它们)调用此函数的代码将boolean:podeJogarTrio转换为true,并且函数正常工作。但是在我宣布它是假的之后,我仍然可以继续点击卡片并在1秒延迟(动画)中移除它们以移除卡片!有任何想法吗?? =))

function verlaranja()
{
    if(podeJogarTrio == true)
    {
        for(var u:int=0; u < 3; u++)
        {
            var mcLaranja:Laranja = new Laranja();
            mcLaranja.x = larguraTrio + (mcLaranja.width + 5) * (u % 3);
            mcLaranja.y = alturaTrio + (mcLaranja.height + 5) * (Math.floor(u/3));
            mcLaranja.gotoAndStop(u + 1);
            mcLaranja.buttonMode = true;
            mcLaranja.addEventListener(MouseEvent.CLICK, cliquelaranja);
            mcExplic.addChild(mcLaranja);
            trio.push(mcLaranja);

            function cliquelaranja(e:MouseEvent)
            { 
                e.currentTarget.nextFrame();

                var teste1:int = trio[0].currentFrame;
                var teste2:int = trio[1].currentFrame;
                var teste3:int = trio[2].currentFrame;

                if((teste1 == 3) && (teste2 == 3) && (teste3 == 3))
                {
                    podeJogarTrio = false;    // NOT WORKING PROPERLY!
                    intervalo = setInterval(removeLaranja, intervaloTrio);
                    function removeLaranja()
                    {
                        clearInterval(intervalo);

                        for(var k:int=0; k < 3; k++)
                        {
                            trio[k].removeEventListener(MouseEvent.CLICK, cliquelaranja);
                            mcExplic.removeChild(trio[k]);
                            trace(trio);
                            if(k == 2)
                            {
                                removeTrio();
                            }
                        }
                    }
                }
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

关于避免嵌套函数和setInterval(你应该检查计时器类)的注释都值得考虑。

您遇到的问题是,在执行间隔之后才删除事件侦听器,因此,在此期间发生单击时,附加到此侦听器的代码仍会运行。有几种方法可以解决这个问题,但最简单的方法可能是将条件移动到事件监听器函数:

function verlaranja()
{

    for(var u:int=0; u < 3; u++)
    {
        var mcLaranja:Laranja = new Laranja();
        mcLaranja.x = larguraTrio + (mcLaranja.width + 5) * (u % 3);
        mcLaranja.y = alturaTrio + (mcLaranja.height + 5) * (Math.floor(u/3));
        mcLaranja.gotoAndStop(u + 1);
        mcLaranja.buttonMode = true;
        mcLaranja.addEventListener(MouseEvent.CLICK, cliquelaranja);
        mcExplic.addChild(mcLaranja);
        trio.push(mcLaranja);

        function cliquelaranja(e:MouseEvent)
        {
            // I think you want to check your boolean here
            if (podeJogarTrio) 
            {
                e.currentTarget.nextFrame();

                var teste1:int = trio[0].currentFrame;
                var teste2:int = trio[1].currentFrame;
                var teste3:int = trio[2].currentFrame;

                if((teste1 == 3) && (teste2 == 3) && (teste3 == 3))
                {
                    podeJogarTrio = false;    // NOT WORKING PROPERLY!
                    intervalo = setInterval(removeLaranja, intervaloTrio);
                    function removeLaranja()
                    {
                        clearInterval(intervalo);

                        for(var k:int=0; k < 3; k++)
                        {
                            trio[k].removeEventListener(MouseEvent.CLICK, cliquelaranja);
                            mcExplic.removeChild(trio[k]);
                            trace(trio);
                            if(k == 2)
                            {
                                removeTrio();
                            }
                        }
                    }
                }
            }
        }
    }
}