gpt4 book ai didi

LibGDX And​​roid 游戏 - 在滚动屏幕上显示固定文本(即分数)

转载 作者:行者123 更新时间:2023-12-02 04:47:51 24 4
gpt4 key购买 nike

我开始用 LibGDX 编写游戏,才刚刚开始。我已经加载了一个基本的瓷砖 map ,一个玩家 Sprite 并且可以移动角色并且屏幕(相机)可以滚动 - 完美。

我在屏幕右下角有两个重叠的纹理,一个左右箭头,用作控制角色的 handle 。我根据 players.x 位置定位它们,该位置始终固定在屏幕中心。到目前为止很酷....如下所示:

/////////////////////////////////////////////////////////////////
private void renderJoypad(float deltaTime)
{
batch.draw(Assets.leftJoypad, blockman.position.x-14, 1, 3, 3);
batch.draw(Assets.rightJoypad, blockman.position.x-9, 1, 3, 3);
}

我现在试图将玩家的分数放在屏幕的左上角。乐谱由 Bitmap 字体组成,如下所示。

font = new BitmapFont(Gdx.files.internal("fonts/minecrafter.fnt"),Gdx.files.internal("fonts/minecrafter.png"),false);
font.setScale(0.02f);

在我的 render() 方法中,我调用了一些其他方法来更新,比如leftJoypad 等,如在 renderJoypad() 中。我还调用我的绘制字体方法来更新乐谱的位置,但是,当我滚动时,它总是不稳定,有时它显示的字符少于应有的字符。

public void drawScore(float deltaTime) 
{
font.draw(batch, "00000", blockman.position.x, 10);
}

我认为我需要将乐谱(以及任何其他屏幕文本、HUD 等)放入舞台中,但我不明白如何让它与我现有的代码一起工作。

我的展示方法如下:

public void show()
{
//load assets class to get images etc
Assets Assets = new Assets();

//start logging Framerate
Gdx.app.log( GameScreen.LOG, "Creating game" );
fpsLogger = new FPSLogger();

//set the stage - bazinga

Gdx.input.setInputProcessor(stage);

//stage.setCamera(camera);
//stage.setViewport(480, 360, false);

batch = new SpriteBatch();

//load the Joypad buttons
loadJoypad();

//background image
loadBackground();

//sounds
jumpSound = Gdx.audio.newSound(Gdx.files.internal("sounds/slime_jump.mp3"));
//LOAD block man here

// load the map, set the unit scale to 1/16 (1 unit == 16 pixels)
loadMap();

//draw the score
font = new BitmapFont(Gdx.files.internal("fonts/minecrafter.fnt"),Gdx.files.internal("fonts/minecrafter.png"),false);
font.setScale(0.02f);


// create an orthographic camera, shows us 30x20 units of the world
camera = new OrthographicCamera();
camera.setToOrtho(false, 30, 20);
camera.update();

// create the Blockman we want to move around the world
blockman = new Blockman();
blockman.position.set(25, 8);
}

我的 render() 方法如下:

public void render(float delta)
{
// get the delta time
float deltaTime = Gdx.graphics.getDeltaTime();

stage.act(deltaTime);
stage.draw();

// clear the screen
Gdx.gl.glClearColor(0.3f, 0.3f, 0.3f, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
renderBackground(deltaTime);

batch = renderer.getSpriteBatch();

//updateBlockman(deltaTime);
blockman.update(deltaTime);

// let the camera follow the blockMan, x-axis only
camera.position.x = blockman.position.x;
camera.update();

// set the tile map rendere view based on what the
// camera sees and render the map
renderer.setView(camera);
renderer.render();

//start the main sprite batch
batch.begin();


// render the blockMan
renderBlockman(deltaTime);
renderJoypad(deltaTime);
drawScore(deltaTime);

batch.end();
fpsLogger.log();

}

我试图改变与 Spritebatch 等相关的事情的工作方式,但似乎无法按照我的要求让它工作。

任何人都可以建议我如何让舞台和 Actor 工作,或者第二台摄像机或其他东西来帮助我在角落里实现固定的分数显示。

我是否需要使用 Scene 2D 或其他东西 - 啊啊啊!我的头在爆炸....

我期待着并提前感谢您。

问候

詹姆斯

最佳答案

我有几个建议:

  1. 检查您是否有 setUseIntegerPositions设置为真(默认)当您绘制字体时。如果你这样做,你正在扩展它,然后它会导致一些奇怪的效果类似于你描述。将其设置为 false,看看是否能解决问题。

  2. 在绘制文本之前重置 spritebatch 的矩阵,这样您就不需要为滚动调整它。

如果可以的话,我什至建议不要缩放字体,因为缩放后字体通常看起来有点奇怪。

关于LibGDX And​​roid 游戏 - 在滚动屏幕上显示固定文本(即分数),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19450627/

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