0

所以,这是有问题的代码:

function moveObsticle():void
    {
        //move
        var tempObs:MovieClip;
        for(var i:int = obsticles.length-1; i>=0; i--)
        {
            tempObs = obsticles[i];
            tempObs.y = tempObs.y - playerSpeed;
        }

        //test if obsticle is off-stage and set it to remove
        if (tempObs != null && tempObs.y < stage.stageHeight)
        {
            removeObsticle(i);
        }

        //player-obsticle colision
        if (tempObs != null && tempObs.hitTestObject(player))
        {
            gameState = STATE_END;
        }
    }

这是我代码中的 moveX 函数之一,它们都有相同的问题。因此,此功能在程序(游戏)开始时完美运行,但是在玩游戏 30 秒或一分钟后,hitTestObject() 停止工作,我的游戏只是失去了所有的游戏元素。

所以有问题的代码是函数末尾的 if 语句,但我怀疑 mby 一个 for 循环也可能是一个问题,但是 hitTest 上面的 if 语句(测试 obs 是否不在舞台......)确实工作得很好。

这个错误让我发疯,我已经开发了一个带有该错误的整个游戏,现在是时候摆脱它了,我找不到任何人有同样的问题,而且我以前从未遇到过这个问题。

代码在 AIR for Android 中运行,整个过程在 Adob​​e Flash Pro cs6 中开发

4

2 回答 2

0

尝试将此代码更改为以下内容:

function moveObsticle():void
{
    //move
    var tempObs:MovieClip;
    for(var i:int = obsticles.length-1; i>=0; i--)
    {
        tempObs = obsticles[i];
        tempObs.y = tempObs.y - playerSpeed;

        //test if obsticle is off-stage and set it to remove
        if (tempObs != null && tempObs.y < stage.stageHeight)
        {
            removeObsticle(i);
            continue;
        }

        //player-obsticle colision
        if (tempObs != null && tempObs.hitTestObject(player))
        {
            gameState = STATE_END;
        }
    }
}
于 2013-07-01T13:34:07.010 回答
0

通过将代码更改为以下代码解决了问题(想法来自@jfgi):

function moveObsticle():void
    {
        //move
        var tempObs:MovieClip;
        for(var i:int = obsticles.length-1; i>=0; i--)
        {
            tempObs = obsticles[i];
            tempObs.y = tempObs.y - playerSpeed;

             //player-obsticle colision
             if (tempObs != null && tempObs.hitTestObject(player))
            {
            gameState = STATE_END;
            }
        }

        //test if obsticle is off-stage and set it to remove
        if (tempObs != null && tempObs.y < stage.stageHeight)
        {
            removeObsticle(i);
        }
    }

谢谢@jfgi!

于 2013-07-01T17:18:05.030 回答