我的渲染方法:
public void render(float delta) {
Gdx.gl.glClearColor(1, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
int w = GateRunner.WIDTH;
int h = GateRunner.HEIGHT;
cam.update();
cam.setToOrtho(false, w, h);
Gdx.input.setInputProcessor(this);
game.batch.begin();
game.batch.draw(title_background, 0, 0, GateRunner.WIDTH, GateRunner.HEIGHT);
game.batch.draw(title, (w / 2) - (w / 2), h / 2 + h * 12 / 90, w, (int) (w * 0.5));
playButtonSprite.setPosition((w / 2) - (w / 2), h / 2 + h / 20);
playButtonSprite.setSize(w, (int) (w * (144.0 / 1080)));
playButtonSprite.draw(game.batch);
game.batch.draw(instructions_button, (w/2) - (w/2), h/2 + h/20 - h*3/40, w, (int) (w * (144.0 / 1080)));
game.batch.draw(about_button, (w / 2) - (w / 2), h / 2 + h/20 - 2*h*3/40, w, (int)(w*(144.0/1080)));
game.batch.end();
}
我的touchDown方法。
public boolean touchDown(int screenX, int screenY, int pointer, int button) {
float pointerX = InputTransform.getCursorToModelX(windowWidth, screenX);
float pointerY = InputTransform.getCursorToModelY(windowHeight, screenY);
if(playButtonSprite.getBoundingRectangle().contains(pointerX, pointerY)) //Play button
{
game.setScreen(new PlayScreen(game));
dispose();
}
return true;
}
当我点击Play“按钮”(这只是一个Sprite)时,我想这样做,它会转移到PlayScreen。我通过检查是否点击了Play精灵的矩形来做到这一点。但是,即使这部分有效,每当我点击PlayScreen中的那个矩形区域时,它再次运行代码 - 它会启动PlayScreen。我该如何解决这个问题?
编辑:此外,这个问题可能有更好的名称,所以请随时提出建议。
答案 0 :(得分:3)
这是因为当您更改屏幕时,您仍然使用相同的输入处理器来检查屏幕的矩形是否被点击。按钮是否可见是无关紧要的。
您可以执行其中一项修复...
或者,查看像scene2d.ui这样的框架来为你处理这类东西。