gpt4 book ai didi

java - LibGdx,渲染计数屏幕上的整数

转载 作者:行者123 更新时间:2023-12-02 03:57:25 25 4
gpt4 key购买 nike

现在程序可以计数到 31,我喜欢让它在屏幕上呈现 count 整数变量。我尝试了 batch.draw(Integer.toString(count)); ,也尝试了 font.draw(batch, Integer.toString(count), 25, 160); 都在 JAVA 中给了我错误(语法错误)。请给我一些建议。谢谢~

package com.mygdx.game;

import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.InputAdapter;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;

public class Prac1 extends ApplicationAdapter {
private float w,h,tw,th =0;
private OrthographicCamera camera;
private SpriteBatch batch;
private Sprite img;
private int count;


@Override
public void create () {
w = Gdx.graphics.getWidth();
h = Gdx.graphics.getHeight();
camera = new OrthographicCamera(w, h);
camera.position.set(w/2, h/2, 0);
camera.update();
batch = new SpriteBatch();
img = new Sprite(new Texture(Gdx.files.internal("iceCream.png")));
tw = img.getWidth();
th = img.getHeight();
img.setBounds(camera.position.x - (tw/2), camera.position.y - (th/2),tw,th);
count=0;


Gdx.input.setInputProcessor(new InputAdapter(){

@Override
public boolean touchDown(int screenX, int screenY, int pointer, int button) {

if(img.getBoundingRectangle().contains(screenX, screenY) && count<=31)
System.out.println(count++);

return true;
}
});
}

@Override
public void render () {
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
batch.begin();
img.draw(batch);
batch.end();
}
}

最佳答案

您需要声明一个位图字体并在您的创建方法中初始化它(看起来您忘记了这样做):

private BitmapFont font;
private SpriteBatch batch;

public void create() {
batch = new SpriteBatch();
font = new BitmapFont();
font.setColor(Color.BLACK);
}

@Override
public void render() {
batch.begin();
font.draw(batch, String.valueOf(count), 200, 200); //you can change the position as you like
batch.end();
}

并替换

System.out.println(count++);

count++

关于java - LibGdx,渲染计数屏幕上的整数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35303242/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com