我正在开发一个脆弱的鸟类克隆,我真的陷入了我想创建的这个按钮。对不起,我知道这是一个基本问题,但我是初学者,我真的需要你的帮助。所以,游戏已经完成了,但是我没有触摸整个屏幕上的鸟类来跳跃,我想制作一个按钮来做同样的事情。这是我的InputHandler类:
public class InputHandler implements InputProcessor {
private Bird myBird;
private GameWorld myWorld;
// Ask for a reference to the Bird when InputHandler is created.
public InputHandler(GameWorld myWorld) {
// myBird now represents the gameWorld's bird.
this.myWorld = myWorld;
myBird = myWorld.getBird();
}
@Override
public boolean touchDown(int screenX, int screenY, int pointer, int button) {
if (myWorld.isReady()) {
myWorld.start();
}
myBird.onClick();
if (myWorld.isGameOver() || myWorld.isHighScore()) {
// Reset all variables, go to GameState.READ
myWorld.restart();
}
return true;
}
@Override
public boolean keyDown(int keycode) {
return false;
}
@Override
public boolean keyUp(int keycode) {
return false;
}
@Override
public boolean keyTyped(char character) {
return false;
}
@Override
public boolean touchUp(int screenX, int screenY, int pointer, int button) {
return false;
}
@Override
public boolean touchDragged(int screenX, int screenY, int pointer) {
return false;
}
@Override
public boolean mouseMoved(int screenX, int screenY) {
return false;
}
@Override
public boolean scrolled(int amount) {
return false;
}
}
我在GameScreen类中创建了一个舞台和一个演员(按钮)(它不起作用),它在屏幕上显示,但我不知道接下来该做什么。我再次对这个基本问题感到抱歉,但我真的需要你的帮助来完成这个游戏。
public class GameScreen implements Screen {
private OrthographicCamera cam;
private int gameHeight;
Stage stage;
TextureAtlas buttonAtlas;
TextButtonStyle buttonStyle;
TextButton button;
Skin skin;
BitmapFont font;
SpriteBatch batch;
private GameWorld world;
private GameRenderer renderer;
private float runTime;
// This is the constructor, not the class declaration
public GameScreen() {
float screenWidth = Gdx.graphics.getWidth();
float screenHeight = Gdx.graphics.getHeight();
float gameWidth = 544;
float gameHeight = screenHeight / (screenWidth / gameWidth);
world = new GameWorld();
renderer = new GameRenderer(world, (int) gameHeight, (int) gameWidth);
Gdx.input.setInputProcessor(new InputHandler(world));
}
@Override
public void render(float delta) {
runTime += delta;
world.update(delta);
renderer.render(runTime);
cam = new OrthographicCamera();
cam.setToOrtho(false, 544, gameHeight);
batch = new SpriteBatch();
batch.setProjectionMatrix(cam.combined);
stage.act();
batch.begin();
stage.draw();
batch.end();
}
@Override
public void resize(int width, int height) {
System.out.println("GameScreen - resizing");
}
@Override
public void show() {
System.out.println("GameScreen - show called");
stage = new Stage (Gdx.graphics.getWidth(), Gdx.graphics.getHeight(), true);
font = new BitmapFont(Gdx.files.internal("data/font.fnt"), false);
skin = new Skin();
buttonAtlas = new TextureAtlas ("data/button.pack");
skin.addRegions(buttonAtlas);
buttonStyle = new TextButtonStyle();
buttonStyle.up = skin.getDrawable("button");
buttonStyle.over = skin.getDrawable("button - Copy");
buttonStyle.down = skin.getDrawable("button - Copy");
buttonStyle.font = font;
button = new TextButton("" ,buttonStyle);
stage.addActor(button);
}
@Override
public void hide() {
System.out.println("GameScreen - hide called");
}
@Override
public void pause() {
System.out.println("GameScreen - pause called");
}
@Override
public void resume() {
System.out.println("GameScreen - resume called");
}
@Override
public void dispose() {
// Leave blank
}
}
答案 0 :(得分:1)
你几乎可以摆脱任何引用一般触摸输入的东西。完成后,您需要使用按钮的OnClick()
方法调用"反弹"小鸟。如果您已经将此功能作为其自身的方法,则应该相对容易。
要离开"为你做这件事,我会在C#中给你一个例子:
boingButton_Click(Object sender, EventArgs e)
{
bird.Bounce();
}
反弹方法就是这样:
Bounce()
{
bird.Height += 4;
bird.Descend();
}
这可以假设你有一种鸟类下降的方法。按钮的优点是理想情况下你不应该为它的方法带来任何重载,尽管我可能在Java上出错了。
答案 1 :(得分:0)
Wolfish的想法是对的。转换为Libgdx框架的内容如下:
btnBounce.addListener( new ClickListener() {
@Override
public void clicked(InputEvent event, float x, float y) {
bird.bounce();
};
});