我仍然是libgdx中的新手并且很困惑,我想在我的游戏中使用一些按钮(总是在左下角的屏幕中)来制作一些事件(使用项目,创建士兵等)。我在哪里可以将这个按钮放在我的游戏课程中?我有worldController类,worldRenderer类和GameScreen类(比如来自libgdx电子书的canyon bunny) 我尝试在worldRenderer类中添加stage和button,然后在renderGui方法之一中绘制它,但没有显示任何内容。那么,如何在我的游戏世界中插入按钮? 感谢...
public class WorldRenderer implements Disposable {
private OrthographicCamera camera;
public SpriteBatch batch;
private Stage stage = new Stage();
private Button btnItem;
private Skin skinLibgdx = new Skin(
Gdx.files.internal(Constants.SKIN_LIBGDX_UI),
new TextureAtlas(Constants.TEXTURE_ATLAS_LIBGDX_UI));
private WorldController worldController;
private OrthographicCamera cameraGUI;
public float w = Constants.VIEWPORT_GUI_WIDTH/1366;
public float h = Constants.VIEWPORT_GUI_HEIGHT/768;
public WorldRenderer (WorldController worldController) {
this.worldController = worldController;
init();
}
private void init () {
batch = new SpriteBatch();
camera = new OrthographicCamera(Constants.VIEWPORT_WIDTH, Constants.VIEWPORT_HEIGHT);
camera.position.set(0, 0, 0);
camera.update();
btnItem = new Button(skinLibgdx);
btnItem.addListener(new ClickListener() {
@Override
public void clicked(InputEvent event, float x, float y) {
// TODO Auto-generated method stub
System.out.println("ABC");
}
});
stage.addActor(btnItem);
//background = new ParallaxBackground(layers,camera,batch);
cameraGUI = new OrthographicCamera(Constants.VIEWPORT_GUI_WIDTH,
Constants.VIEWPORT_GUI_HEIGHT);
cameraGUI.position.set(0, 0, 0);
cameraGUI.setToOrtho(true); // flip y-axis
cameraGUI.update();
}
public void render () {
renderWorld(batch);
renderGui(batch);
}
private void renderWorld (SpriteBatch batch) {
//background.render();
worldController.cameraHelper.applyTo(camera);
batch.setProjectionMatrix(camera.combined);
batch.begin();
worldController.level.render(batch);
batch.end();
}
public void resize (int width, int height) {
//camera.viewportWidth = (Constants.VIEWPORT_HEIGHT / height) *width;
camera.update();
cameraGUI = new OrthographicCamera(Constants.VIEWPORT_GUI_WIDTH,
Constants.VIEWPORT_GUI_HEIGHT);
cameraGUI.position.set(0, 0, 0);
cameraGUI.setToOrtho(true); // flip y-axis
cameraGUI.update();
}
@Override
public void dispose() {
// TODO Auto-generated method stub
batch.dispose();
}
private void renderGuiScore (SpriteBatch batch) {
float x = -15;
float y = -15;
batch.draw(Assets.instance.enemies.soldiers,x, y, 50, 50, 100, 100, 0.35f, -0.35f, 0);
Assets.instance.fonts.defaultBig.draw(batch,
"" + worldController.score,
x + 75, y + 37);
//stage.draw(); i have try draw in this line too but UI become dissapear
}
private void renderGuiExtraLive (SpriteBatch batch) {
float x = cameraGUI.viewportWidth - 50 - Constants.LIVES_START*50;
float y = -15*h;
for (int i = 0; i < Constants.LIVES_START; i++) {
if (worldController.lives <= i)
batch.setColor(0.5f, 0.5f, 0.5f, 0.5f);
batch.draw(Assets.instance.hero.hero1,
x + i * 50, y, 50, 50, 120, 100, 0.35f, -0.35f, 0);
batch.setColor(1, 1, 1, 1);
}
}
private void renderGuiHealth (SpriteBatch batch) {
float x = 50;
float y = -15*h;
for (int i = 0; i < Constants.LIVES_START; i++) {
if (worldController.lives <= i)
batch.setColor(Color.RED);
float health = worldController.level.hero.getHealth();
Assets.instance.fonts.defaultBig.draw(batch,
"" + Math.round(health),
x + 75, y + 37);
}
}
private void renderGuiEnergy (SpriteBatch batch) {
float x = 125;
float y = -15*h;
for (int i = 0; i < Constants.LIVES_START; i++) {
if (worldController.lives <= i)
batch.setColor(Color.GREEN);
float energy = worldController.level.hero.getEnergy();
Assets.instance.fonts.defaultBig.draw(batch,
"" + Math.round(energy),
x + 75, y + 37);
}
}
private void renderGuiPlasma (SpriteBatch batch) {
float x = 200;
float y = -15*h;
for (int i = 0; i < Constants.LIVES_START; i++) {
if (worldController.lives <= i)
batch.setColor(Color.BLACK);
Assets.instance.fonts.defaultBig.draw(batch,
"" + worldController.level.hero.getUltimateEnergy() + "-" + worldController.level.hero.getPower() + "-" + worldController.plasma,
x + 75, y + 37);
}
}
private void renderGui (SpriteBatch batch) {
batch.setProjectionMatrix(cameraGUI.combined);
batch.begin();
// draw collected gold coins icon + text
// (anchored to top left edge)
renderGuiScore(batch);
// draw extra lives icon + text (anchored to top right edge)
renderGuiExtraLive(batch);
//draw Health
renderGuiHealth(batch);
//draw Energy
renderGuiEnergy(batch);
//draw Plasma
renderGuiPlasma(batch);
batch.end();
stage.draw();
}
}
答案 0 :(得分:0)
你忘记了:
stage.act();
设置输入处理器:
Gdx.input.setInputProcessor(stage);