как сделать простую кнопку в libGDX?
Делаю простенькую игру под андроид на libGDX, и сейчас понадобилось сделать кнопки для управления, до этого тестировал приложение в десктопном варианте, привязав действия к клавишам, попробовал добавить кнопку, но вместо этого просто черно-белая полоса на экране.
package com.mygdx.game.Screens;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.maps.tiled.TiledMap;
import com.badlogic.gdx.maps.tiled.TmxMapLoader;
import com.badlogic.gdx.maps.tiled.renderers.OrthogonalTiledMapRenderer;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.Box2DDebugRenderer;
import com.badlogic.gdx.physics.box2d.World;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Table;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
import com.badlogic.gdx.utils.viewport.ExtendViewport;
import com.badlogic.gdx.utils.viewport.FitViewport;
import com.badlogic.gdx.utils.viewport.Viewport;
import com.mygdx.game.Main;
import com.mygdx.game.Scenes.Hud;
import com.mygdx.game.Sprites.Samurai;
import com.mygdx.game.Tools.B2WorldCreator;
import com.mygdx.game.Tools.WorldContactListener;
public class PlayScreen implements Screen {
private Main game;
private TextureAtlas atlas;
private Stage stage;
private TextButton button;
private TextButton.TextButtonStyle textButtonStyle;
private BitmapFont font;
private OrthographicCamera gameCam;
private Viewport gamePort;
private Hud hud;
private TmxMapLoader mapLoader;
private TiledMap map;
private OrthogonalTiledMapRenderer renderer;
private World world;
private Box2DDebugRenderer b2dr;
private Samurai player;
public PlayScreen(Main game) {
atlas = new TextureAtlas("Fighters/Samurai/Samurai.pack");
this.game = game;
gameCam = new OrthographicCamera();
gamePort = new FitViewport(Main.V_WIDTH / Main.PPM, Main.V_HEIGHT / Main.PPM, gameCam);
hud = new Hud(game.batch);
mapLoader = new TmxMapLoader();
map = mapLoader.load("map/map.tmx");
renderer = new OrthogonalTiledMapRenderer(map, 1 / Main.PPM);
gameCam.position.set(gamePort.getWorldWidth() / 2, gamePort.getWorldHeight() / 2, 0);
world = new World(new Vector2(0, -10), true);
b2dr = new Box2DDebugRenderer();
// b2dr.setDrawBodies(false);
player = new Samurai(world, this);
new B2WorldCreator(world, map);
world.setContactListener(new WorldContactListener());
stage = new Stage(gamePort);
Gdx.input.setInputProcessor(stage);
font = new BitmapFont();
textButtonStyle = new TextButton.TextButtonStyle();
textButtonStyle.font = font;
button = new TextButton("BUTTON", textButtonStyle);
Table table = new Table();
table.add(button);
table.row();
table.setFillParent(true);
table.center().center();
stage.addActor(table);
}
public TextureAtlas getAtlas() {
return atlas;
}
@Override
public void show() {
}
public void handleInput(float dt) {
if (Gdx.input.isKeyJustPressed(Input.Keys.SPACE)) {
player.b2body.applyLinearImpulse(new Vector2(0, 2.5f), player.b2body.getWorldCenter(), true);
}
if (Gdx.input.isKeyPressed(Input.Keys.D) && player.b2body.getLinearVelocity().x <= 2) {
player.b2body.applyLinearImpulse(new Vector2(0.1f, 0), player.b2body.getWorldCenter(), true);
}
if (Gdx.input.isKeyPressed(Input.Keys.A) && player.b2body.getLinearVelocity().x >= -2) {
player.b2body.applyLinearImpulse(new Vector2(-0.1f, 0), player.b2body.getWorldCenter(), true);
}
}
public void update(float dt) {
handleInput(dt);
player.update(dt);
world.step(1/60f, 6, 2);
gameCam.position.x = player.b2body.getPosition().x;
gameCam.update();
renderer.setView(gameCam);
}
@Override
public void render(float delta) {
update(delta);
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
renderer.render();
b2dr.render(world, gameCam.combined);
game.batch.setProjectionMatrix(gameCam.combined);
game.batch.begin();
player.draw(game.batch);
game.batch.end();
stage.act(Gdx.graphics.getDeltaTime());
stage.draw();
}
@Override
public void resize(int width, int height) {
gamePort.update(width, height);
}
@Override
public void pause() {
}
@Override
public void resume() {
}
@Override
public void hide() {
}
@Override
public void dispose() {
map.dispose();
renderer.dispose();
world.dispose();
b2dr.dispose();
hud.dispose();
}
}
Ответы (1 шт):
Автор решения: SurfaceStack
→ Ссылка
Добавьте в переменные:
private Stage stage;
private TextButton button;
private TextButton.TextButtonStyle textButtonStyle;
private BitmapFont font;
private Skin button_skin;
private TextureAtlas button_textureAtlas;
PlayScreen():
public PlayScreen(Main game) {
atlas = new TextureAtlas("Fighters/Samurai/Samurai.pack");
this.game = game;
gameCam = new OrthographicCamera();
gamePort = new FitViewport(Main.V_WIDTH / Main.PPM, Main.V_HEIGHT / Main.PPM, gameCam);
hud = new Hud(game.batch);
mapLoader = new TmxMapLoader();
map = mapLoader.load("map/map.tmx");
renderer = new OrthogonalTiledMapRenderer(map, 1 / Main.PPM);
gameCam.position.set(gamePort.getWorldWidth() / 2, gamePort.getWorldHeight() / 2, 0);
world = new World(new Vector2(0, -10), true);
b2dr = new Box2DDebugRenderer();
// b2dr.setDrawBodies(false);
player = new Samurai(world, this);
new B2WorldCreator(world, map);
world.setContactListener(new WorldContactListener());
//Кнопка:
stage = new Stage();
Gdx.input.setInputProcessor(stage);
font = new BitmapFont();
button_skin = new Skin();
button_textureAtlas = new TextureAtlas(Gdx.files.internal("interface/buttons_atlas.atlas"));
button_skin.addRegions(button_textureAtlas);
textButtonStyle = new TextButtonStyle();
textButtonStyle.font = font;
textButtonStyle.up = button_skin.getDrawable("Button"); //Не нажатая кнопка
textButtonStyle.down = button_skin.getDrawable("Button_pressed"); //Нажатая кнопка
button = new TextButton("Play", textButtonStyle);
button.setSize(button.getWidth() * 16, button.getHeight() * 2); //Размер кнопки, скорее всего надо изменить
//Место кнопки на экране:
button.setPosition(Gdx.graphics.getWidth() / 2 - button.getWidth() / 2, Gdx.graphics.getHeight() - button.getHeight() * 2);
stage.addActor(button);
//Обработчик нажатия кнопки
button.addListener(new ChangeListener() {
@Override
public void changed(ChangeEvent event, Actor actor) {
//ваше действие
}
});
//------
}
render():
@Override
public void render(float delta) {
update(delta);
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
renderer.render();
b2dr.render(world, gameCam.combined);
game.batch.setProjectionMatrix(gameCam.combined);
game.batch.begin();
player.draw(game.batch);
game.batch.end();
stage.draw();
}