【问题标题】:Libgdx show score and add 1 to score every secondLibgdx 显示得分并每秒加 1 得分
【发布时间】:2014-11-05 15:03:33
【问题描述】:

我想将分数每秒增加 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() {

}
}

【问题讨论】:

  • 转储源代码并说“让它这样做”并不是在 stackoverflow.com 上获得有用答案的最佳方式。您可能需要考虑查看stackoverflow.com/help/how-to-ask,然后以不同的方式提出您的问题......
  • 你可以像以前一样增加它,制作一个位图字体并绘制它。它不是很困难只是谷歌它。如果你想让它居中,你可以使用位图字体来获取宽度并将其除以 2,然后从屏幕宽度的一半中减去它。想要一个例子吗?

标签: java android timer libgdx scoring


【解决方案1】:
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
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-11-15
    • 2021-04-17
    • 2018-03-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-31
    相关资源
    最近更新 更多