简体   繁体   中英

Display elapsed time in screen as Score java libgdx

I'm new in game programming and I'm building one with libgdx in Android Studio .

I want the score to be the same as the elapsed time in the PlayState .

How can I place it in the top right corner with the "score" label next to it?

Please help! I'm stucked!

This is a little bit of code I've made in the PlayState .

    public class PlayState extends State {
    private Texture bg;
    private Texture ground;

    long startTime;

    public static BitmapFont font;
    private int score;
    private String scoreText;


    public PlayState(GameStateManager gsm) {
        super(gsm);
        bg = new Texture("bg.png");
        ground = new Texture("ground.png");

        startTime = System.currentTimeMillis();

        font = new BitmapFont(Gdx.files.internal("font.fnt"));
        font.getData().setScale(.25f, .25f);
    }

    @Override
    protected void handleInput() {

    }

    @Override
    public void update(float dt) {
        handleInput();

       cam.update();
    }

    @Override
    public void render(SpriteBatch sb) {
        sb.setProjectionMatrix(cam.combined);
        sb.begin();
        sb.draw(bg, cam.position.x - (cam.viewportWidth / 2), 0);
        System.out.println("Score = " + ((System.currentTimeMillis() - startTime) / 100));
        sb.end();
    }

    @Override
    public void dispose() {
        bg.dispose();
        ground.dispose();
        System.out.println("Play State Disposed");
    }
}

In your xml, place a TextView in the top right corner. You can use a Handler so that you can update the time and UI concurrently. Here is something I am using now..

Your XML would look something like this

<TextView
   android:id="@+id/txt_Timer"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:layout_alignParentRight="true"
   android:layout_marginRight="5dp" />

I put that in my FrameLayout

In your activity you can launch the Timer by doing this...

//Define global variable for time elapse and TextView
private int timeElapsed = 0;
private TextView txt_Timer;

//In the onCreate method define
txt_Timer = (TextView) findViewById(R.id.txt_View);
txt_Timer.setText(Integer.toString(timeElapsed));

//Place this wheree
Handler handler = new Handler();
Runnable r = new Runnable() {
public void run() {
    //Update and display
    timeElapsed += 1;
    txt_Timer.setText(Integer.toString(timeElapsed));
    //Call this again in one second (1000 milliseconds)
    handler.postDelayed(this, 1000);
    }
};

You may want to declare the Runnable globally so you can access elsewhere in the class. And when you want to start the timer you can call....

handler.postDelayed(r, 1000);

If you want to pause the timer you can call....

handler.removeCallbacks(r);

I don't know if this will solve your problem but it should working, assuming you are in an activity. I never made a mobile game with libgdx...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM