如何在AndEngine中制作计时器?

3

我正在制作一款赛车游戏,现在我想要实现一个时钟/计时器来显示你的时间。

我想要在一个ChangeableText中显示实际时间,在另一个ChangeableText中显示最佳圈速时间。

如何每秒或每0.1秒更新时间?

如何在完成一圈时检测到,例如在终点线(确定的x和y位置)?

谢谢。

2个回答

7

试试这个:

int count=60;
youScene.registerUpdateHandler(new TimerHandler(1f, true, new ITimerCallback() {
        @Override
        public void onTimePassed(TimerHandler pTimerHandler) {
                count--;
                youchangeabletext.setText(String.valueof(count));  
                if(count==0){
                 youScene.unregisterUpdateHandler(pTimerHandler);
                 //GameOver();
                 }        
               pTimerHandler.reset();

        }
}));

参数 "1f" 是方法运行的时间,此时 1f = 1 秒,现在每秒运行一次该方法,当计数为 "0" 时,该方法将从游戏中移除。

现在,要检测圈数是否完成,您可以扩展汽车的 Sprite 类:

public class Car extends AnimatedSprite {
            public Car(final float pX, final float pY, final TiledTextureRegion pTextureRegion, final VertexBufferObjectManager pVertexBufferObjectManager) {
                super(pX, pY, pTextureRegion, pVertexBufferObjectManager);
            }
//the method onManagedUpdate run in milliseconds
            @Override
            protected void onManagedUpdate(final float pSecondsElapsed) {
                 //for example when car is in the position 100 in x
                if(this.getX()>=100){
                          //lap finish
                }
                super.onManagedUpdate(pSecondsElapsed);
            }
        }

0
int time=110;

timerText = new Text(700, 440, resourcesManager.font, "10", new TextOptions(HorizontalAlign.RIGHT), vbom);

TimerHandler mTime = new TimerHandler(0.1f,true, new ITimerCallback(){

     @Override

     public void onTimePassed(TimerHandler pTimerHandler) {

          // TODO Auto-generated method stub

          time--;

          timerText.setText(""+(time/10));

          if(time==0){

             //Do something

          }

    }

 });

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接