我想检测两个角色之间的碰撞,所以我创建了两个矩形,使用{rectangle1.overlaps(rectangle2)}方法检测碰撞/重叠。我想在它们重叠时调用GameOver场景,因此我在{public void render()}方法中调用了overlays方法。问题是我遇到了StackOverfLow错误。我的GameMain类扩展了Game类,而GameOver类实现了Screen类。 这是我在render方法类中调用的contactDetection方法:
public void contactDetection(){
if (bounds.overlaps(boundscake)) {
setScreen(new GameOver(score, this));
}
}
这是我的GameOver类:
public class GameOver implements Screen {
private int score;
Game game;
SpriteBatch batch;
Texture texture;
GameMain gameMain;
public GameOver(int score, Game game){
this.score = score;
this.game = game;
texture = new Texture("test.jpg");
batch = new SpriteBatch();
gameMain = new GameMain();
}
@Override
public void show() {
game.setScreen(this);
}
@Override
public void render(float delta) {
Gdx.gl.glClearColor(0,0,0,1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
batch.begin();
batch.draw(texture, 0,0 ,Gdx.graphics.getWidth(),Gdx.graphics.getHeight());
batch.end();
}