How to change TimerHandler delay in AndEngine?

前端 未结 4 1569
余生分开走
余生分开走 2021-01-01 07:49

I have this method i create that adds a sprite to my andengine scene every second.

 private void createSpriteSpawnTimeHandler(){
        TimerHandler spriteT         


        
相关标签:
4条回答
  • 2021-01-01 08:20

    Store the handler in an instance variable, and call its setTimerSeconds method when needed.

    0 讨论(0)
  • 2021-01-01 08:30

    Override onCreateEngine method with new FixedStepEngine

    @Override
    public Engine onCreateEngine(EngineOptions pEngineOptions) {
    
        return new FixedStepEngine(pEngineOptions, 30);
    }
    
    0 讨论(0)
  • 2021-01-01 08:35

    I have created this class just now for you. I think it should work, and you can change the interval between ticks as you wish. (Haven't tested it, so you might need to make some small fixes. But it looks fine IMO.)

    Use it this way:

    Timer timer = new Timer(interval, new Timer.ITimerCallback() {
        public void onTick() {
            //Your code to execute each interval.
        }
    }
    (Engine object here).registerUpdateHandler(timer);
    

    If you ever want to change the interval, you should keep a reference to the timer. Anyways, to change interval use:

    timer.setInterval(newInterval);
    
    0 讨论(0)
  • 2021-01-01 08:36

    Looking at the TimerHandler source file here: http://code.google.com/p/andengine/source/browse/src/org/anddev/andengine/entity/handler/timer/TimerHandler.java?r=30ba07312ed7d4636077b835ba15f772e3f30db7

    You'll see that there isn't a method to update the interval. And the variable mTimerSeconds is also final.

    I would implement my own IUpdateHandler class and add a non-final mTimerSeconds and a method to update it.

    You can copy the existing TimerHandler and then make the changes accordingly.

    0 讨论(0)
提交回复
热议问题