user2498848
user2498848

Reputation:

Slick2d java game crashing when I setBackground();

I'm using slick2d and in my Java application. In my render method I use a method called changeBackground();

@Override
public void render(GameContainer gc, StateBasedGame sbg, Graphics g)
        throws SlickException {
changeBackground(g);
}

changeBackground();

public void changeBackground(Graphics g) throws SlickException{
    Thread.sleep(500);
    g.setBackground(new org.newdawn.slick.Color(0, 255, 0);
    Thread.Sleep(500);
    g.setBackground(new org.newdawn.slick.Color(255, 0, 0);
    changeBackground(g);
}

When I run my application the game crashes.

Upvotes: 0

Views: 203

Answers (1)

NG.
NG.

Reputation: 22904

You are probably getting a stack overflow.

public void changeBackground(Graphics g) throws SlickException{
    Thread.sleep(500);
    g.setBackground(new org.newdawn.slick.Color(0, 255, 0);
    Thread.Sleep(500);
    g.setBackground(new org.newdawn.slick.Color(255, 0, 0);
    changeBackground(g); // you're calling this funct again!  BAD
}

Remove the last line and hopefully you won't crash in that spot.

Upvotes: 3

Related Questions