【问题标题】:Displaying score in libgdx game在 libgdx 游戏中显示分数
【发布时间】:2016-01-25 01:54:38
【问题描述】:

所以我无法在我正在创建的游戏中保持分数显示。我想知道我该怎么做?这是我目前所拥有的:

package com.catgame.game;

import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.InputProcessor;
import com.badlogic.gdx.audio.Music;
import com.badlogic.gdx.audio.Sound;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;

public class CatGame extends ApplicationAdapter implements InputProcessor {
SpriteBatch batch;
Sprite cat;
OrthographicCamera camera;
final float CAT_WIDTH = 0.75f;
final float CAT_HEIGHT = 0.50f;
Sound meow;

int score = 0;
String scorePrint;
BitmapFont scoreFont;


@Override
public void create () {
    batch = new SpriteBatch();
    cat = new Sprite(new Texture(Gdx.files.internal("Cat.png")));
    cat.setSize(CAT_WIDTH,CAT_HEIGHT);
    scoreFont = new BitmapFont(Gdx.files.internal("score.fnt"));
    float aspectRatio = (float)Gdx.graphics.getHeight()/
            (float)Gdx.graphics.getWidth();
    scoreFont.getData().setScale(0.5f);


    camera = new OrthographicCamera(CAT_HEIGHT * aspectRatio,
                                    CAT_HEIGHT);
    camera.position.set(CAT_WIDTH/2,CAT_HEIGHT/2,0);

    meow = Gdx.audio.newSound(Gdx.files.internal("Meow.wav"));

    Gdx.input.setInputProcessor(this);

    scorePrint = "Hello";


}

@Override
public void render () {
    Gdx.gl.glClearColor(0, 0, 0, 1);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
    camera.update();
    batch.begin();
    batch.setProjectionMatrix(camera.combined);
    //cat.draw(batch);
    scoreFont.draw(batch,scorePrint,camera.position.x,camera.position.y);
    batch.end();
}

我尝试了 scoreFont.draw() 方法,但它似乎不起作用。不知道为什么,也许我的定位是错误的,因为我有一个正射相机或其他东西。但是当我尝试使用绘图字体时,什么也没有出现。下图中的红色圆圈显示了我想要得分的位置。

http://imgur.com/gallery/ywFF6SZ/new

【问题讨论】:

    标签: java android libgdx 2d-games


    【解决方案1】:

    在渲染方法中绘制字体并更改字体颜色或背景颜色。这可能是因为您的黑色背景颜色与 BitmapFont 颜色匹配。

    【讨论】:

    • 字体为白色,背景为黑色
    【解决方案2】:

    您需要在渲染函数中绘制分数(或您想在屏幕上看到的任何内容),因为每次渲染都会清除画布并重新绘制。

    【讨论】:

    • 刚刚更新了抱歉。现在我为中心设置了它,但最终想把它移过来。但即使这样也没有显示出来。
    【解决方案3】:

    你画的字体太大了。如果在绘制字体之前没有指定字体比例,它会以一个字体像素到一个世界单位的比例绘制它。在您的情况下,您的相机高度小于 1,因此您只能看到文本中第一个字母的一小部分像素,这可能只是空白空间。设置好相机后,在字体上调用setScale,使用适当的比例将其缩小到您想要的大小。

    【讨论】:

    • 所以我基本上会做我对精灵所做的事情,除了创建函数中的 setScale 吗??
    • 是的。 -------------------
    • 我刚试过这个,还是看不到。我应该只是缩小图像,甚至不使用相机来做这样的事情吗???我已经更新了上面的代码以显示我的尝试。也许我没有正确缩放字体??
    • 尝试同时设置font.setUseIntegerPositions(false);。但这会使字母的边缘略微模糊。确实,字体被设计为完美像素(与绘制其他所有内容的正确方法相反)。如果您在 UI 中使用它们,最简单的方法是为您的 UI 相机使用 ScreenViewport,这样您就不必缩放字体。如果你必须将它们混合到你的游戏场景中(所以它们不能有自己的相机),那么就需要缩放它们并关闭整数位置,但在这种情况下,你应该对字体使用三线性过滤以最大限度地减少模糊.
    • 感谢您的帮助。现在我正在尝试 setUseIntegerPosition(false) 但现在它只是让应用程序完全崩溃。不知道为什么@Tenfour04
    猜你喜欢
    • 2016-03-03
    • 2016-03-18
    • 1970-01-01
    • 2013-10-27
    • 2011-11-10
    • 2018-06-17
    • 1970-01-01
    • 2018-08-12
    • 1970-01-01
    相关资源
    最近更新 更多