LibGdx,Render在屏幕上计数整数

时间:2016-02-09 22:12:55

标签: java libgdx

现在程序可以将数字计算到31,我喜欢在屏幕上呈现count整数变量。我尝试batch.draw(Integer.toString(count));并尝试了font.draw(batch, Integer.toString(count), 25, 160);两者都在JAVA中给了我错误(语法错误)。请给我一些建议。谢谢〜

package com.mygdx.game;

import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.InputAdapter;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;

public class Prac1 extends ApplicationAdapter {
    private float w,h,tw,th =0;
    private OrthographicCamera camera;
    private SpriteBatch batch;
    private Sprite img;
    private int count;


    @Override
    public void create () {
        w = Gdx.graphics.getWidth();
        h = Gdx.graphics.getHeight();
        camera = new OrthographicCamera(w, h);
        camera.position.set(w/2, h/2, 0);
        camera.update();
        batch = new SpriteBatch();
        img = new Sprite(new Texture(Gdx.files.internal("iceCream.png")));
        tw = img.getWidth();
        th = img.getHeight();
        img.setBounds(camera.position.x - (tw/2), camera.position.y - (th/2),tw,th);
        count=0;


        Gdx.input.setInputProcessor(new InputAdapter(){

            @Override
            public boolean touchDown(int screenX, int screenY, int pointer, int button) {

                if(img.getBoundingRectangle().contains(screenX, screenY) && count<=31) 
                    System.out.println(count++);

                return true;
            }
        });
    }

    @Override
    public void render () {
        Gdx.gl.glClearColor(0, 0, 0, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
        batch.begin();
        img.draw(batch);
        batch.end();
    }
}

1 个答案:

答案 0 :(得分:2)

你需要声明一个位图字体并在你的create方法中初始化它(看起来你忘记这样做了):

var fb = new FacebookClient();
dynamic result = fb.Get("oauth/access_token", new { 
    client_id     = "app_id", 
    client_secret = "app_secret", 
    grant_type    = "client_credentials" 
});
fb.AccessToken = result.access_token;

并替换

private BitmapFont font;
private SpriteBatch batch;

public void create() {        
    batch = new SpriteBatch();    
    font = new BitmapFont();
    font.setColor(Color.BLACK);
}

@Override
public void render() {        
    batch.begin();
    font.draw(batch, String.valueOf(count), 200, 200); //you can change the position as you like
    batch.end();
}

System.out.println(count++);