Java Libgdx显示分数并每秒增加1到分数
我想每秒钟增加1分,但我正在努力让它正常工作 e、 g (伪代码): 我还想知道如何在我的屏幕顶部和中间显示分数 我的完整源代码:Java Libgdx显示分数并每秒增加1到分数,java,android,timer,libgdx,scoring,Java,Android,Timer,Libgdx,Scoring,我想每秒钟增加1分,但我正在努力让它正常工作 e、 g (伪代码): 我还想知道如何在我的屏幕顶部和中间显示分数 我的完整源代码: 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;
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() {
}
}
package com.ryanwarren.dodge.game;
导入com.badlogic.gdx.ApplicationAdapter;
导入com.badlogic.gdx.gdx;
导入com.badlogic.gdx.Input.Keys;
导入com.badlogic.gdx.graphics.GL20;
导入com.badlogic.gdx.graphics.Texture;
导入com.badlogic.gdx.graphics.g2d.SpriteBatch;
导入com.badlogic.gdx.math.Vector2;
公共类libgdxGame扩展了ApplicationAdapter{
喷雾批;
纹理播放器;
矢量2位置;
浮动时间=0f;
@凌驾
public void create(){
批次=新的SpriteBatch();
player=新纹理(Gdx.files.internal(“player.png”);
位置=新矢量2((Gdx.graphics.getWidth()/2-(player.getWidth()/2)),50);
}
@凌驾
公共空间处置(){
}
@凌驾
公共无效渲染(){
Gdx.gl.glClearColor(1,1,1,1);
Gdx.gl.glClear(GL20.gl\u颜色\u缓冲\u位);
如果(Gdx.input.isKeyPressed(Keys.W)){
位置y+=1f;
}
如果((Gdx.input.isKeyPressed(Keys.A))&&(position.x>0)){
位置x-=2f;
}
如果(Gdx.input.isKeyPressed(Keys.S)){
位置y-=1f;
}
如果((Gdx.input.isKeyPressed(Keys.D))&&(position.x=0)和&(position.x>0)){
位置x-=2f;
}
如果((Gdx.input.getAccelerometerX()<0)和&(position.x
在stackoverflow.com上,转储源代码并按照“让它这样做”的思路说一些话并不是获得有用答案的最佳方式。你可能想考虑一下,然后用不同的方式来设置你的问题…你可以像你一样增加它并制作一个BITMAPFIDEND并画出来。谷歌搜索并不难。如果你想让它居中,你可以使用位图字体得到宽度,然后除以2,然后从屏幕宽度的一半中减去。想举个例子吗?在stackoverflow.com上,转储源代码并说一些类似“让它这样做”的话并不是获得有用答案的最佳方式。你可能想考虑一下,然后用不同的方式来设置你的问题…你可以像你一样增加它并制作一个BITMAPFIDEND并画出来。谷歌搜索并不难。如果你想让它居中,你可以使用位图字体得到宽度,然后除以2,然后从屏幕宽度的一半中减去。想举个例子吗?
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
}