Search code examples
andengine

Game freezes for a few seconds, even when everything is in handlers


I have been testing my game for 3-4 devices, from which one is lower end device.

The game freezes for a few sends,then the items appear on screen.First I thought its because is low end device.But i found only once, on first time installation that in S4 too,the freezed for a sec and then it was smooth.So I guess problem is in my code.

I have kept everything in TimerHandler and collusion and score calculation in Update handler,I am not able to find where the problem is. Are TimeHandler and UpdateHandler different from Thread,that we create?

Please find snippet of my code(cant post whole code)

private void collectPointsTimeHandler() {

    mEngine.unregisterUpdateHandler(spriteTimerHandler);
    spriteTimerHandler = null;

    float mEffectSpawnDelay = 1.8f;

    normalTimerHandler = new TimerHandler(mEffectSpawnDelay, true,
            new ITimerCallback() {

        public void onTimePassed(TimerHandler pTimerHandler) {

            //seconds calculation

            if(seconds % 2 == 0 || seconds % 3 == 0){
                addNegativeItems();
                getEngine().unregisterUpdateHandler(wellTimerHandler);
            }else if(seconds % 5 == 0){                 
                addQuestion();                      
            }
        }
    });     

    mEngine.registerUpdateHandler(normalTimerHandler);
}

This function is called in onLoadScene();

Then the collusion checking part

IUpdateHandler pointsHandler = new IUpdateHandler() {

    public void reset() {
    }

    public void onUpdate(float pSecondsElapsed) {

        Iterator<AnimatedSprite> targets = targetPoint.iterator();
        AnimatedSprite _target  = null;

        // iterating over the targets
        while (targets.hasNext()) {
            _target = targets.next();
            // if target passed the left edge of the screen, then remove it
            // and call a fail]
            if (_target.getY() >= cameraHeight) {
                // removeSprite(_target, targets);
                _target.setVisible(false);
                _target.detachSelf();
                _target = null;
                targets.remove();
                break;
            }

            if(well.checkRectangle.collidesWith(_target))
            {
                @SuppressWarnings("unchecked")
                ArrayList<Integer> userData = (ArrayList<Integer>)_target.getUserData();
                manageEffects(userData);

                _target.setVisible(false);
                _target.detachSelf();
                _target.clearEntityModifiers();
                _target.clearUpdateHandlers();
                _target.reset();
                _target = null;
                targets.remove();   
            }   
        }           
        targetPoint.addAll(collectedPoint);
        collectedPoint.clear();


        checkCompletedScore();  
    }       
};

Any idea where,why the the game freeze everytime a new item is added on screen.


Solution

  • You need to use sprite pool. I had similar problem ,by using sprite pool i could fix it

    http://c0deattack.wordpress.com/2011/01/06/andengine-using-the-object-pool/