joanlopez
joanlopez

Reputation: 589

How to make a chronometer in AndEngine?

I'm making a Race Game, and now I want to implement a clock/chronometer that shows your times.

I want to show the actual time in a ChangeableText and the Best lap time in another ChangeableText.

How I can update the time every second or every 0.1 seconds ??

How I can detect when a lap finishes, for exemple in a finishing line (determinate x and y position)??

Thanks.

Upvotes: 3

Views: 1714

Answers (2)

Vince
Vince

Reputation: 1

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

          }

    }

 });

Upvotes: 0

Vinicius DSL
Vinicius DSL

Reputation: 1859

Try this:

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();

        }
}));

he parameter "1f" is the time to run the method in this case 1f = 1 second, now each second run the method and when the count is "0" the method is removed from game.

Now for detect a lap finishes, you can extend Sprite class of you car:

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

Upvotes: 7

Related Questions