簡體   English   中英

Libgdx顯示得分並每秒加1以得分

[英]Libgdx show score and add 1 to score every second

我想每秒將分數提高1分,但我正在努力使其正常工作。

例如

(偽代碼):

int score = 0f // on create

updateEverySecond() {
    score += 1;
    displayScore()
}

我還想知道如何將分數顯示在屏幕頂部並居中。

我的完整源代碼:

package com.ryanwarren.dodge.game;

import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Vector2;

public class libgdxGame extends ApplicationAdapter {

SpriteBatch batch;
Texture player;

Vector2 position;

float time = 0f;

@Override
public void create () {     
    batch = new SpriteBatch();

    player = new Texture(Gdx.files.internal("player.png"));

    position = new Vector2((Gdx.graphics.getWidth()/2 - (player.getWidth()/2)),50);     
}

@Override
public void dispose() {

}

@Override
public void render () {
    Gdx.gl.glClearColor(1, 1, 1, 1);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

    if(Gdx.input.isKeyPressed(Keys.W)) {
        position.y += 1f;
    }
    if((Gdx.input.isKeyPressed(Keys.A)) && (position.x > 0)) {
        position.x -= 2f;
    }
    if(Gdx.input.isKeyPressed(Keys.S)) {
        position.y -= 1f;
    }
    if((Gdx.input.isKeyPressed(Keys.D))  && (position.x < Gdx.graphics.getWidth() - player.getWidth())) {
        position.x += 2f;
    }

    if(Gdx.input.isTouched()) {
        System.out.println("application clicked");
    }

    if((Gdx.input.getAccelerometerX() >= 0) && (position.x > 0)) {
        position.x -= 2f;
    }
    else if((Gdx.input.getAccelerometerX() < 0) && (position.x < Gdx.graphics.getWidth() - player.getWidth())) {
        position.x += 2f;
    }

    System.out.println("Rotation: " + Gdx.input.getAccelerometerX());

    batch.begin();
    batch.draw(player, position.x, position.y);
    batch.end();
}

@Override
public void resize(int width, int height) {

}

@Override
public void pause() {

}

@Override
public void resume() {

}
}
float timeState=0f; 

public void render(){
// ............
timeState+=Gdx.graphics.getDeltaTime();
if(timeState>=1f){
// 1 second just passed
   timeState=0f; // reset our timer
    updateEverySecond(); // call the function that you want
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM