我想在遊戲中添加一個分數,它應該以分數:0開始,每當對象被擊中時將分數加1。字體正在工作,並顯示「得分:」[不是0] 碰撞起作用。libgdx添加分數BitmapFont
public class PlayState extends State {
private int score;
private String scoreName;
private BitmapFont font;
public PlayState(GameStateManager gsm) {
super(gsm);
score = 0;
scoreName = "score: 0";
font = new BitmapFont(Gdx.files.internal("font.fnt"));
}
@Override
public void update(float dt) {
handleInput();
if(ObjectA.collides(ObjectB.getBounds()))
scoreName = "score: " + ++score;
}
@Override
public void render(SpriteBatch sb) {
sb.setProjectionMatrix(cam.combined);
sb.begin();
font.setColor(0.5f, 0.5f, 0.5f, 0.5f);
font.getData().setScale(0.4f);
font.draw(sb, scoreName, cam.position.x, cam.position.y);
sb.end();
}
ApplicationAdapter類
public class XY extends ApplicationAdapter {
public static final int WIDTH = 480;
public static final int HEIGHT = 800;
public static final String TITLE = "XY";
private GameStateManager gsm;
private SpriteBatch batch;
@Override
public void create() {
batch = new SpriteBatch();
gsm = new GameStateManager();
Gdx.gl.glClearColor(1, 0, 0, 1);
gsm.push(new MenuState(gsm));
}
@Override
public void render() {
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
gsm.update(Gdx.graphics.getDeltaTime());
gsm.render(batch);
}
@Override
public void dispose() {
super.dispose();
}
GameStateManager
public class GameStateManager {
private Stack<State> states;
public GameStateManager(){
states = new Stack<State>();
}
public void push(State state){
states.push(state);
}
public void pop(){
states.pop().disposed();
}
public void set(State state){
states.pop().disposed();
states.push(state);
}
public void update(float dt){
states.peek().update(dt);
}
public void render(SpriteBatch sb){
states.peek().render(sb);
}
}
當分數爲0時,您需要它顯示「分數:0」,分數是1時是否顯示「分數:1」? –
是的,並且當對象A被擊中時每次加1 +1 – Tim272793
你的意思是「它不適合」,還有什麼是這個分數中的「不是0」:「[不是0]' – Aryan