gpt4 book ai didi

java - LibGDX - tile/tilemaps 中的纹理文本

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:15:44 26 4
gpt4 key购买 nike

有什么方法可以在 Tilemaps 中将文本放在纹理上?

我正在开发一款基于跳棋的游戏,我需要显示单个方 block 中有多少跳棋。我使用 TiledMap 加载电路板。我的代码如下:

GameScreen.java

public class GameScreen implements Screen {

private TiledMap board;

public GameScreen(final MyGame gam) {

board = new TmxMapLoader().load("maps/tablero-64.tmx");
renderer = new OrthogonalTiledMapRenderer(board, 1/64f);

boardCamera = new OrthographicCamera();
boardCamera.setToOrtho(false, WIDTH, HEIGHT);
boardCamera.position.set(CENTERX, CENTERY, 0);

// Added this to avoid continuous rendering
Gdx.graphics.setContinuousRendering(false);
Gdx.graphics.requestRendering();
}

@Override
public void render(float delta) {
boardCamera.update();
renderer.setView(boardCamera);
renderer.render();
renderer.getSpriteBatch().begin();
for (Stack checker : whiteCheckers) checker.draw(renderer.getSpriteBatch(), 1);
for (Stack checker : blackCheckers) checker.draw(renderer.getSpriteBatch(), 1);
renderer.getSpriteBatch().end();
}
}

Stack.java

public class Stack extends Actor {

static BitmapFont font = new BitmapFont(Gdx.files.internal("fonts/estragelo.fnt"), Gdx.files.internal("fonts/estragelo.png"), true); // Line 31

@Override
public void draw(SpriteBatch batch, float parentAlpha) {
batch.draw(this.texture,
this.boardPosition.x,
this.boardPosition.y,
this.size,
this.size);
// This DOESN'T work
font.draw(batch, "10", this.boardPosition.x, this.boardPosition.y); // Line 296
}
}

提前致谢。

编辑:我添加错误日志:

12-24 17:49:18.178: E/AndroidRuntime(3365): FATAL EXCEPTION: GLThread
12-24 17:49:18.178: E/AndroidRuntime(3365): java.lang.ExceptionInInitializerError
12-24 17:49:18.178: E/AndroidRuntime(3365): at my.package.GameScreen.<init>(GameScreen.java:81)
12-24 17:49:18.178: E/AndroidRuntime(3365): at my.package.MainMenuScreen.render(MainMenuScreen.java:36)
12-24 17:49:18.178: E/AndroidRuntime(3365): at com.badlogic.gdx.Game.render(Game.java:46)
12-24 17:49:18.178: E/AndroidRuntime(3365): at my.package.MyGame.render(MyGame.java:23)
12-24 17:49:18.178: E/AndroidRuntime(3365): at com.badlogic.gdx.backends.android.AndroidGraphics.onDrawFrame(AndroidGraphics.java:487)
12-24 17:49:18.178: E/AndroidRuntime(3365): at com.badlogic.gdx.backends.android.surfaceview.GLSurfaceViewCupcake$GLThread.guardedRun(GLSurfaceViewCupcake.java:713)
12-24 17:49:18.178: E/AndroidRuntime(3365): at com.badlogic.gdx.backends.android.surfaceview.GLSurfaceViewCupcake$GLThread.run(GLSurfaceViewCupcake.java:646)
12-24 17:49:18.178: E/AndroidRuntime(3365): Caused by: com.badlogic.gdx.utils.GdxRuntimeException: Error loading font file: fonts/estragelo.fnt
12-24 17:49:18.178: E/AndroidRuntime(3365): at com.badlogic.gdx.graphics.g2d.BitmapFont$BitmapFontData.<init>(BitmapFont.java:971)
12-24 17:49:18.178: E/AndroidRuntime(3365): at com.badlogic.gdx.graphics.g2d.BitmapFont.<init>(BitmapFont.java:135)
12-24 17:49:18.178: E/AndroidRuntime(3365): at com.badlogic.gdx.graphics.g2d.BitmapFont.<init>(BitmapFont.java:127)
12-24 17:49:18.178: E/AndroidRuntime(3365): at my.package.Stack.<clinit>(Stack.java:31)
12-24 17:49:18.178: E/AndroidRuntime(3365): ... 7 more
12-24 17:49:18.178: E/AndroidRuntime(3365): Caused by: java.lang.ArrayIndexOutOfBoundsException
12-24 17:49:18.178: E/AndroidRuntime(3365): at com.badlogic.gdx.graphics.g2d.BitmapFont$BitmapFontData.<init>(BitmapFont.java:836)
12-24 17:49:18.178: E/AndroidRuntime(3365): ... 10 more

编辑 2:问题是字体。 Estragelo 字体生成两个 PNG 文件,但您只能在 BitmapFont 构造函数中指定一个。所以我改成 Arial 字体,它起作用了。但是现在我遇到了与开始时相同的问题。这给了我与这样做相同的结果:

public void draw(SpriteBatch batch, float parentAlpha) {
BitmapFont font = new BitmapFont();
font.draw(batch, "10", this.boardPosition.x, this.boardPosition.y);
}

在某种程度上,它正在绘制数字,但位置和大小都不好。这是结果(boardPosition 是 (4,0) 和 (3,7),其中放置了跳棋):

This is the new result

最佳答案

新答案:

由于您的新BitmapFont 字体是静态的,它会在Gdx 初始化之前加载。这就是为什么它不起作用。为避免这种情况,您可以将静态字段 font 留空并在 GameScreen 构造函数中分配它:

public class Stack extends Actor {

static BitmapFont font;

@Override
public void draw(SpriteBatch batch, float parentAlpha) {
//your draw stuff
}
}

然后在构造函数中分配字体:

public class GameScreen implements Screen {

public GameScreen(final MyGame gam) {
//your initializers and settings
Stack.font=new BitmapFont(Gdx.files.internal("fonts/estragelo.fnt"), Gdx.files.internal("fonts/estragelo.png"), true);
}

旧答案:

您必须使用 Hiero 或 [BMFont]( http://www.angelcode.com/products/bmfont/) 创建字体并使用生成的文件:

font=new BitmapFont(Gdx.files.local("fonts/font.fnt"),
Gdx.files.local("fonts/font.png"), true);

在我的例子中,字体文件位于我工作区的子文件夹“fonts”中。

在 BMFont 中,您可以选择字体的大小、样式等。

编辑:您应该考虑 noncontinuous rendering对于游戏来说,因为棋盘游戏没有必要每一帧都重新绘制图形。

关于java - LibGDX - tile/tilemaps 中的纹理文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20735262/

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