简体   繁体   中英

Displaying score in libgdx game

So i am having trouble keeping a display of the score in a game i am creating. I was wondering how would i go about doing that? this is what i have so far:

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();
}

I have tried scoreFont.draw() method but it doesn't seem to work. not sure why, maybe my positioning is wrong since I have an ortho camera or something. But When i tried to used the draw font, nothing appeared. The red circle in the image below shows where i would want the score around.

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

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

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

You are drawing the font too big. When you don't specify a scale for the font before drawing it, it draws it at a scale of one font pixel to one world unit. In your case, your camera height is less than one, so you are only seeing a fraction of one pixel of the first letter in your text, which is likely just empty space. Call setScale on the font after setting up your camera, using the appropriate scale to get it down to the size you want.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM