LibGDX Android游戏-在滚动屏幕上显示固定文本(ie分数)
我开始用LibGDX写一个游戏,才刚刚开始。我有一个基本的瓦片地图加载,一个玩家精灵,可以移动周围的角色和屏幕(相机)滚动-完美 我在屏幕的右下角有两个重叠纹理,一个左箭头和一个右箭头,它们被用作操纵台来控制角色。我将这些位置与players.x位置相对应,该位置始终固定在屏幕中央。到目前为止很酷。。。。详情如下:LibGDX Android游戏-在滚动屏幕上显示固定文本(ie分数),libgdx,sprite,Libgdx,Sprite,我开始用LibGDX写一个游戏,才刚刚开始。我有一个基本的瓦片地图加载,一个玩家精灵,可以移动周围的角色和屏幕(相机)滚动-完美 我在屏幕的右下角有两个重叠纹理,一个左箭头和一个右箭头,它们被用作操纵台来控制角色。我将这些位置与players.x位置相对应,该位置始终固定在屏幕中央。到目前为止很酷。。。。详情如下: ///////////////////////////////////////////////////////////////// private void renderJoypad
/////////////////////////////////////////////////////////////////
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);
}
我现在试着把球员的分数放在屏幕左上角。分数由位图字体组成,如下所示
font = new BitmapFont(Gdx.files.internal("fonts/minecrafter.fnt"),Gdx.files.internal("fonts/minecrafter.png"),false);
font.setScale(0.02f);
在render()方法中,我调用了一些其他方法来更新,比如
leftJoypad等,如renderJoypad()中的。我还调用了我的draw font方法来更新分数的位置,但是,当我滚动时,它是不稳定的,有时它显示的字符比应该显示的少
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();
}
我的render()方法如下所示:
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还是什么-啊哈!我的头爆炸了
我期待并提前感谢你
问候
詹姆斯我有几个建议:
如果你能帮忙的话,我甚至建议你不要缩放字体,因为缩放后字体通常看起来有点奇怪。哇,谢谢你。我将其设置为font.setUseIntegerPositions(false);正如你所建议的,它工作得很好。真不敢相信。你知道当设置为true时,它实际上做了什么导致它出错吗?只是为了进一步理解。我非常感激,非常感谢。james当设置为true时,将强制字体绘制为整数坐标。如果您正在绘制像素,这很好,但是如果您的视口缩放为30x20,那么结果可能会有点奇怪。玩得好。我花了一天的时间什么也没看到。setuseIntegerPositions至少起作用:我的世界范围是0-3,这使得字体非常难看。。。。