我开始在LibGDX中编写游戏,仅仅是开始。我已经加载了一个基本的瓷砖地图,一个玩家精灵,并且可以移动角色并且屏幕(相机)滚动 - 完美。LibGDX Android游戏 - 在滚动屏幕上显示固定文本(即分数)
我在屏幕右下角有一个左右两个覆盖的纹理,左右箭头用作控制角色的游戏手柄。我将这些与玩家位置相关联,这些位置始终固定在屏幕中央。目前很酷....如下:
/////////////////////////////////////////////////////////////////
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()方法我CAM调用一些其他的方法来更新,像 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);
}
和我的渲染()方法如下:
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等工作方式并且似乎无法按照我的要求工作。
任何人都可以建议我怎样才能获得舞台和演员的工作,或第二个相机或东西,以帮助我在角落实现固定的比分显示。
我是否需要使用场景2D或其他内容 - aahhh!我的头正在爆炸....
我期待着,并提前谢谢你。
问候
詹姆斯
哇,谢谢你。我将它设置为font.setUseIntegerPositions(false);正如你所建议的那样,它完美运作。我无法相信。你是否知道它在设置为真时的实际情况,导致它出错 - 只是为了进一步理解。我非常感谢,非常感谢。 James –
当它设置为true时,它强制字体被绘制为整数坐标。如果你正在绘制像素,这很好,但是如果你的视口缩放到30x20那么结果可能会有点奇怪。 –
发挥得很好。我一天都没有看到任何东西。 setuseIntegerPositions至少起作用:我的世界比例为0-3,这使得字体非常难看...... – RichieHH