1

如何创建一个在 AndEngine 中重复 n 次的时间处理程序

我正在使用它,但它只能工作一次

private void createSpriteSpawnTimeHandler() {
    TimerHandler spriteTimerHandler;


    spriteTimerHandler = new TimerHandler(5, new ITimerCallback() {

                @Override
                public void onTimePassed(TimerHandler pTimerHandler) {

                    initHardware();
                }
            });

    getEngine().registerUpdateHandler(spriteTimerHandler);
}
4

3 回答 3

2
private TimerHandler spriteTimerHandler;

private void createSpriteSpawnTimeHandler() {

    spriteTimerHandler = new TimerHandler(5, true, new ITimerCallback() {

            int counter = 0;

            @Override
            public void onTimePassed(TimerHandler pTimerHandler) {
                counter++;
                initHardware();
                if(counter >= 5) {
                    mEngine.unregisterUpdateHandler(spriteTimerHandler);
                }
            }
        });

    getEngine().registerUpdateHandler(spriteTimerHandler);
}
于 2013-05-31T07:09:55.380 回答
0

但它运行一次然后我改变了

org.andengine.engine.handler.timer 中的 TimerHandler.java 库文件

     public TimerHandler(final float pTimerSeconds, final ITimerCallback pTimerCallback) {
    this(pTimerSeconds, false, pTimerCallback);
}

 public TimerHandler(final float pTimerSeconds, final ITimerCallback pTimerCallback) {
    this(pTimerSeconds, true, pTimerCallback);
}

现在它无限工作

于 2013-06-01T07:04:44.617 回答
0
TimerHandler abc;
 abc=new TimerHandler(1, true, new ITimerCallback() {
              int counter=0;
            @Override
            public void onTimePassed(final TimerHandler pTimerHandler) {
                counter++;
                if(counter >= 60) {
                    mEngine.unregisterUpdateHandler(abc);
                    }
                elapsedText.setText("Seconds elapsed: " +counter);
                //fpsText.setText("FPS: " + fpsCounter.getFPS());
            }
        });
          getEngine().registerUpdateHandler(abc);
于 2015-09-04T10:50:58.817 回答