Search code examples
actionscript-3flashflash-cs6hittest

as3 hitTestObject not working after some time


so, this is the code in question:

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;
        }
    }

This is one of moveX functions in my code, they all have the same problem. So this function works perfectly at the begining of the program (game) however after playing a game for mby 30sec or a minute the hitTestObject() just stops working and my game just looses all its gameplay elements.

So the code in question is an if statement at the end of a function, but Im suspecting that mby a for loop might also be a problem, however the if statement above the hitTest one (test if obs is off stage...) does work just fine.

This error is driving me crazy, Ive developed a whole game with that error and now its time to get rid of it, I cant find anybody having a same issue and I have never had that issue before.

code is ran in AIR for Android and the whole thing is developed in Adobe Flash Pro cs6


Solution

  • Try changing this code to following:

    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;
            }
        }
    }