我正在使用libgdx。我正在使用此示例https://github.com/oakes/libgdx-examples/tree/master/SuperKoalio/core/src/net/sekao/superkoalio来创建游戏。
我想在屏幕上添加控件,以便用户可以使用按钮向左,向右,向上移动。我使用了这个“控制器”类https://github.com/BrentAureli/ControllerDemo/tree/master/core/src/com/brentaureli/overlaydemo
但是我无法在屏幕上看到任何控件。任何人都可以提供解决方案来解决这个问题。
public class MainScreen implements Screen {
Stage stage;
TiledMap map;
OrthogonalTiledMapRenderer renderer;
OrthographicCamera camera;
Koala koala;
Controller controller;
public void show() {
map = new TmxMapLoader().load("level1.tmx");
final float pixelsPerTile = 16;
renderer = new OrthogonalTiledMapRenderer(map, 1 / pixelsPerTile);
camera = new OrthographicCamera();
controller = new Controller();
stage = new Stage();
stage.getViewport().setCamera(camera);
koala = new Koala();
koala.layer = (TiledMapTileLayer) map.getLayers().get("walls");
koala.setPosition(20, 10);
stage.addActor(koala);
}
public void render(float delta) {
Gdx.gl.glClearColor(0.5f, 0.5f, 1, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
camera.position.x = koala.getX();
camera.update();
if(Gdx.app.getType() == Application.ApplicationType.Android)
controller.draw();
renderer.setView(camera);
renderer.render();
stage.act(delta);
stage.draw();
}
public void dispose() {
}
public void hide() {
}
public void pause() {
}
public void resize(int width, int height) {
camera.setToOrtho(false, 20 * width / height, 20);
controller.resize(width, height);
}
public void resume() {
}
}
控制器类:
public class Controller {
Viewport viewport;
Stage stage;
boolean upPressed, downPressed, leftPressed, rightPressed;
OrthographicCamera cam;
public Controller(){
cam = new OrthographicCamera();
viewport = new FitViewport(800, 480, cam);
stage = new Stage(viewport);
stage.addListener(new InputListener(){
@Override
public boolean keyDown(InputEvent event, int keycode) {
switch(keycode){
case Input.Keys.UP:
upPressed = true;
break;
case Input.Keys.DOWN:
downPressed = true;
break;
case Input.Keys.LEFT:
leftPressed = true;
break;
case Input.Keys.RIGHT:
rightPressed = true;
break;
}
return true;
}
@Override
public boolean keyUp(InputEvent event, int keycode) {
switch(keycode){
case Input.Keys.UP:
upPressed = false;
break;
case Input.Keys.DOWN:
downPressed = false;
break;
case Input.Keys.LEFT:
leftPressed = false;
break;
case Input.Keys.RIGHT:
rightPressed = false;
break;
}
return true;
}
});
Gdx.input.setInputProcessor(stage);
Table table = new Table();
table.left().bottom();
Image upImg = new Image(new Texture("flatDark25.png"));
upImg.setSize(50, 50);
upImg.addListener(new InputListener() {
@Override
public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
upPressed = true;
return true;
}
@Override
public void touchUp(InputEvent event, float x, float y, int pointer, int button) {
upPressed = false;
}
});
Image downImg = new Image(new Texture("flatDark26.png"));
downImg.setSize(50, 50);
downImg.addListener(new InputListener() {
@Override
public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
downPressed = true;
return true;
}
@Override
public void touchUp(InputEvent event, float x, float y, int pointer, int button) {
downPressed = false;
}
});
Image rightImg = new Image(new Texture("flatDark24.png"));
rightImg.setSize(50, 50);
rightImg.addListener(new InputListener() {
@Override
public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
rightPressed = true;
return true;
}
@Override
public void touchUp(InputEvent event, float x, float y, int pointer, int button) {
rightPressed = false;
}
});
Image leftImg = new Image(new Texture("flatDark23.png"));
leftImg.setSize(50, 50);
leftImg.addListener(new InputListener() {
@Override
public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
leftPressed = true;
return true;
}
@Override
public void touchUp(InputEvent event, float x, float y, int pointer, int button) {
leftPressed = false;
}
});
table.add();
table.add(upImg).size(upImg.getWidth(), upImg.getHeight());
table.add();
table.row().pad(5, 5, 5, 5);
table.add(leftImg).size(leftImg.getWidth(), leftImg.getHeight());
table.add();
table.add(rightImg).size(rightImg.getWidth(), rightImg.getHeight());
table.row().padBottom(5);
table.add();
table.add(downImg).size(downImg.getWidth(), downImg.getHeight());
table.add();
stage.addActor(table);
}
public void draw(){
stage.draw();
}
public boolean isUpPressed() {
return upPressed;
}
public boolean isDownPressed() {
return downPressed;
}
public boolean isLeftPressed() {
return leftPressed;
}
public boolean isRightPressed() {
return rightPressed;
}
public void resize(int width, int height){
viewport.update(width, height);
}
}