gpt4 book ai didi

java - 使用 InputHandler 在 libGdx 中创建播放按钮

转载 作者:行者123 更新时间:2023-12-01 10:06:22 25 4
gpt4 key购买 nike

我创建了一款使用 4 种不同 GameStates 的游戏:READY、RUNNING、GAMEOVER 和 HIGHSCORE(GAMEOVER 的变体,除了这个通知玩家已达到高分)。我的问题是,我设置 InputHandler 的方式是,在 GameState.READY 表单中,用户可以触摸屏幕上的任意位置以进入 GameState.RUNNING 表单。我尝试了多种策略来创建 playButton,但似乎没有任何效果。我创建了一个 PlayButton 类:

public class PlayButton {
private Vector2 position;
private Rectangle boundingRectangle;
private int width;
private int height;

private PlayScreen playScreen;

public PlayButton (float x, float y, int width, int height) {
this.width = width;
this.height = height;
position = new Vector2(x, y);
boundingRectangle = new Rectangle();
}
public void update(float delta) {
boundingRectangle.set(position.x,(position.y),width,height);
}
public float getTheX() {
return position.x;
}

public float getY() {
return position.y;
}

public float getWidth() {
return width;
}

public float getHeight() {
return height;
}

public Rectangle getBoundingRectangle(){
return boundingRectangle;
}
}

在我的InputHandler中我尝试了这个:

public InputHandler(GameWrold myWorld, float scaleFactorX, float scaleFactorY){
this.myWorld = myWorld;
mySam = myWorld.getSam();
playButton = new PlayButton(45,gameHeight-75,50,-35);
buttonPlay = myWorld.getPlayButton();
int midPointY = myWorld.getMidPointY();
this.scaleFactorX = scaleFactorX;
this.scaleFactorY = scaleFactorY;
}
@Override
public boolean touchDown(int screenX, int screenY, int pointer, int button) {
Vector2 touchPos = new Vector2();
touchPos.set(Gdx.input.getX(), Gdx.input.getY());
Vector2 tuch = new Vector2(screenX,screenY);
buttonPlay.getBoundingRectangle();
touch = new Rectangle(touchPos.x,touchPos.y,5,5);
if (myWorld.isReady()) {
if(playButton.getBoundingRectangle().contains(touchPos)){
myWorld.start();
}
}
mySam.onClick();

if (myWorld.isGameOver() || myWorld.isHighScore()) {
// Reset all variables, go to GameState.READ
myWorld.restart();
}
return true;
}
}

正如你所看到的,我尝试使用 touch 变量创建一个 Rectangle ,并尝试检查 touch 是否与 playButtonboundingRectangle 发生碰撞,如下所示:

if(Intersector.overlaps(touch,playButton.getBoundingRectangle())){(playButton.getBoundingRectangle().overlaps(playButton.getBoundingRectangle()))

最佳答案

不要发明一个圆圈,也不要破门而入。

使用Scene2d :)

它是一个与 libGDX 完全兼容的简单 UI 框架,允许您用大约一行代码创建按钮、 slider 、窗口和其他小部件。

<小时/>

您会在我上面附加的链接中找到一个很好的描述,但 TLDR 版本是:

  • 创建Skin并使用TexturePacker为此打包纹理图集(通常免费版本就足够了)
  • 创建 Stage适当的Viewport

    Stage stage = new Stage(new FitViewport(WIDTH, HEIGHT));
  • 将舞台设置为输入处理器(这意味着舞台上的所有事件都将被捕获)

    Gdx.input.setInputProcessor(stage);
  • 创建具有适当样式的按钮(在外观中定义)

    Button button = new Button(skin, "style");
  • 附上ClickListener对按钮进行适当的操作

    button.addListener(new ClickListener(){
    @Override
    public void clicked(InputEvent event, float x, float y)
    {
    //some action
    }
    });
  • 设置其位置并将其添加到舞台

    button.setPosition(x, y);
    stage.addActor(button);
  • render() 中触发 act()draw() 阶段的方法

    //render()
    stage.act();
    stage.draw();
  • 您已经赢得了几个小时;)

关于java - 使用 InputHandler 在 libGdx 中创建播放按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36436377/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com