I am making a small game using Slick2D and I run across an issue that makes my background move behind my character (who doesn't move). I'm not sure whether this is an issue with Slick or my code (probably the latter). The most frustrating thing about this issue is that it only occurs sometimes. With the exact same code it works perfectly, but it is completely unpredictable. Any ideas on what I can do? Here's my update method (There's some unfinished stuff in here regarding other aspects of the game, so just ignore that):
public void update(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException {
Input input = gc.getInput();
if (!grounded) {
vertVelocity -= GRAVITY;
charPositionY += (float) vertVelocity;
}
if (input.isKeyDown(Input.KEY_A) && !quit) {
charPositionX += delta * .4f;
}
if (input.isKeyDown(Input.KEY_D) && !quit) {
charPositionX -= delta * .4f;
}
if (input.isKeyDown(Input.KEY_SPACE)) {
if (grounded) {
vertVelocity = 20;
}
grounded = false;
}
if (charPositionY < ground) {
charPositionY = ground;
grounded = true;
}
if (input.isKeyDown(Input.KEY_ESCAPE)) {
quit = true;
}
if (quit) {
if (input.isKeyDown(Input.KEY_R)) {
quit = false;
}
if (input.isKeyDown(Input.KEY_M)) {
sbg.enterState(0);
}
if (input.isKeyDown(Input.KEY_E)) {
System.exit(0);
}
}
if (charPositionX > MAP_LEFT_LIMIT) {
charPositionX = MAP_LEFT_LIMIT;
}
for (int i = 0; i < crates.length; i++) {
int crateShift = -(crates[i].getPosX()) + 428;
if (charPositionX < crateShift && charPositionX > crateShift - crates[i].getWidth() && charPositionY == 0) {
for (int k = 0; k < crates.length - 1; k++) {
charPositionX = crateShift;
}
}
if (onCrate == false && charPositionX < crateShift && charPositionX > crateShift - crates[i].getWidth() && charPositionY > CRATE_HEIGHT_DEFAULT && !grounded) {
System.out.println(crateShift);
System.out.println(crateShift - crates[i].getWidth());
onCrate = true;
System.out.println("ON CRATE");
}else{
onCrate = false;
}
if(!onCrate){
ground = 0;
System.out.println("NOT ON CRATE");
}else{
ground = 100;
}
if (charPositionY < 0) {
charPositionY = 0;
vertVelocity = 0;
}
if (charPositionX > 210 && charPositionX < crates[i].getPosX() - 32 && charPositionY == 0) {
charPositionX = 210;
}
if (charPositionX < - 270 && charPositionX > - 370 && charPositionY <= 75) {
if (!coinCollected) {
score++;
coinCollected = true;
}
}
if (i == crates.length) {
i = 0;
}
}
}