需要帮助才能使用LIBGDX在菜单屏幕中添加按钮

时间:2014-06-23 11:02:31

标签: android libgdx

我有3个不同的屏幕,其中包含启动画面,菜单屏幕和游戏画面之后。飞溅>菜单> Gamestarts。

如何添加图像按钮?

我想在菜单屏幕中实现3个按钮,不知道从哪里开始。

public class MenuScreen implements Screen {


private Spacegame game;
private SpriteBatch batch;
private Sprite sprite;
private Texture texture;
TextureRegion bg,play,spacegamelogo,button;
OrthographicCamera camera;
Vector3 touchPoint;
private Skin buttonskin;
public MenuScreen(Spacegame game)
{
    touchPoint = new Vector3();
    this.game=game;
    batch=new SpriteBatch();

    bg=AssetLoader.bg;
    spacedebrislogo=AssetLoader.spacedebrislogo;
    button=AssetLoader.button;
}
    @Override
public void show() {
    float w = Gdx.graphics.getWidth();
    float h = Gdx.graphics.getHeight();
   camera = new OrthographicCamera(1, h / w);

    sprite = new Sprite(bg);
    sprite.flip(false, true);
    sprite.setSize(1.0f,
            1.0f * sprite.getHeight() / sprite.getWidth() );
            sprite.setOrigin(sprite.getWidth() / 2,
            sprite.getHeight() / 2);
            sprite.setPosition(-sprite.getWidth() / 2,
            -sprite.getHeight() / 2);
}
@Override
public void render(float delta) {

    batch.setProjectionMatrix(camera.combined);
    batch.begin();

    sprite.draw(batch);

    batch.draw(spacedebrislogo, 33, 54, 50, 40);
    batch.end();
    if (Gdx.input.isTouched()) {

        game.setScreen(new GameScreen());
        dispose();
    }

}

1 个答案:

答案 0 :(得分:1)

有很多方法可以做到这一点.. 我会告诉你我的方式。 首先,我创建我的按钮图像,将其添加到资源文件夹并加载纹理区域。 现在我用它做一个精灵。

Sprite button1=new Sprite(myTextureRegion);

要检查按钮是否被触摸,我可以使用精灵中的矩形来检查您是否触摸了图像。 在您的touchUp方法中,您将执行类似

的操作
if(button1.getBoundingRectangle.contains(screenX,screenY))
  // do your thing

为了让我的游戏更有趣,我喜欢在点击时添加一些旋转或缩放我的精灵,所以它看起来更好,你可以玩它,或者你可以制作2个纹理,一个用于触摸,一个用于触摸起来。