Android LIBgdx,使用rectangle检测冲突。render方法中的overlaps(rectangle)方法给出StackOverFlow错误

Android LIBgdx,使用rectangle检测冲突。render方法中的overlaps(rectangle)方法给出StackOverFlow错误,android,libgdx,Android,Libgdx,我想检测两个参与者之间的冲突,所以我创建了两个矩形,使用{rectangle1.overlaps(rectangle2)}方法检测冲突/重叠。我想在它们重叠时调用GameOver场景,所以我在{public void render()}方法中调用重叠方法。问题是我有一个堆栈溢出错误。我的GameMain类扩展了Game类,GameOver类实现了Screen类。 以下是我在render method类中调用的contactDetection方法: public void contactDetec

我想检测两个参与者之间的冲突,所以我创建了两个矩形,使用{rectangle1.overlaps(rectangle2)}方法检测冲突/重叠。我想在它们重叠时调用GameOver场景,所以我在{public void render()}方法中调用重叠方法。问题是我有一个堆栈溢出错误。我的GameMain类扩展了Game类,GameOver类实现了Screen类。 以下是我在render method类中调用的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();

}

我明白,可能是因为检测碰撞的方法被称为每一帧,并给出错误,我不知道还有其他更好的检测碰撞的方法。你知道吗?堆栈溢出是因为你在show()中调用了setScreen(this),当你调用setScreen()时,游戏类会调用它,所以你创建了一个无限循环。删除那行就行了。在render()中检查边界是正确的方法。