I'm working with slick2D to create a test game and right now I'm doing hit detection using rectangles and the intersects method. I created two rectangles for each platform the player comes in contact with; one used to see if the player is hitting the platform from the sides or bottom and another one pixel tall rectangle that is used to see if the character is standing on the platform.
The problem is if I add anything that hast to do with a plattop (the one pixel long rectangles) to the update class like say:
if(playplathit.intersects(plat1top))
{
falling = false;
}
when you enter the state that the platforms are in you get this error:
Fri Dec 13 22:57:31 EST 2013 ERROR:Game.update() failure - check the game code.
org.newdawn.slick.SlickException: Game.update() failure - check the game code.
at org.newdawn.slick.GameContainer.updateAndRender(Ga meContainer.java:669)
at org.newdawn.slick.AppGameContainer.gameLoop(AppGam eContainer.java:411)
at org.newdawn.slick.AppGameContainer.start(AppGameCo ntainer.java:321)
at javagame.Game.main(Game.java:34)
My guess is that I'm some how overloading Java or Slick. I'm also convinced I'm doing hit detection in the worst possible way as I'm not following a guide. Here is the code without any plattop if statements in the update class:
public class Play extends BasicGameState
{
int[] duration = {200,200};
float playerX = 100;
float playerY = 353;
float screenX = 0;
float screenY = 0;
float plat1X = 0;
float plat2X = 174;
float plat3X = 461;
float plat4X = 618;
float plat5X = 881;
float plat6X = 1233;
float plat7X = 1438;
float plat8X = 1754;
float vertspd = 0.0f;
Animation playeranistart, idleani, runforani, runbackani, jumpani;
boolean falling;
boolean jumping;
boolean rightside = false;
boolean leftside = false;
Rectangle playplathit, plat1, plat2, plat3, plat4, plat5, plat6, plat7, plat8, plat1top, plat2top, plat3top, plat4top, plat5top, plat6top, plat7top, plat8top;
public Play(int state)
{
}
public void player()
{
}
public void init(GameContainer gc, StateBasedGame sbg) throws SlickException
{
Image[] idle = {new Image("res/player.png"), new Image("res/player.png")};
idleani = new Animation(idle,duration,false);
Image[] startidle = {new Image("res/player.png"), new Image("res/player.png")};
playeranistart = new Animation(startidle,duration,false);
Image[] run1 = {new Image("res/playerrun1.png"), new Image("res/playerrun1.png")};
runforani = new Animation(run1,duration,false);
Image[] run2 = {new Image("res/playerrun2.png"), new Image("res/playerrun2.png")};
runbackani = new Animation(run2,duration,false);
}
public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException
{
Image lvl1 = new Image("res/testlvl.png");
playplathit = new Rectangle(playerX, playerY, 15, 39);
plat1 = new Rectangle(plat1X, 393, 597, 8);
plat1top = new Rectangle(plat1X, 392, 597, 1);
plat2 = new Rectangle(plat2X, 325, 247, 5);
plat2top = new Rectangle(plat2X, 324, 247, 1);
plat3 = new Rectangle(plat3X, 265, 78, 6);
plat3top = new Rectangle(plat3X, 264, 78, 1);
plat4 = new Rectangle(plat4X, 232, 141, 5);
plat4top = new Rectangle(plat4X, 231, 141, 1);
plat5 = new Rectangle(plat5X, 393, 755, 8);
plat5top = new Rectangle(plat5X, 392, 755, 1);
plat6 = new Rectangle(plat6X, 357, 214, 44);
plat6top = new Rectangle(plat6X, 356, 214, 1);
plat7 = new Rectangle(plat7X, 330, 219, 70);
plat7top = new Rectangle(plat7X, 329, 219, 1);
plat8 = new Rectangle(plat8X, 393, 243, 8);
plat8top = new Rectangle(plat8X, 392, 243, 1);
g.drawImage(lvl1, screenX,screenY);
playeranistart.draw(playerX,playerY);
g.setColor(Color.red);
g.draw(playplathit);
g.draw(plat1);
g.draw(plat2);
g.draw(plat3);
g.draw(plat4);
g.draw(plat5);
g.draw(plat6);
g.draw(plat7);
g.draw(plat8);
g.draw(plat1top);
g.draw(plat2top);
g.draw(plat3top);
g.draw(plat4top);
g.draw(plat5top);
g.draw(plat6top);
g.draw(plat7top);
g.draw(plat8top);
}
public void update(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException
{
Input input = gc.getInput();
if(jumping == false)
{
vertspd = 0;
}
if((input.isKeyDown(Input.KEY_D) == false) || (input.isKeyDown(Input.KEY_A) == false)|| (input.isKeyDown(Input.KEY_W) == false))
{
playeranistart = idleani;
}
if(input.isKeyDown(Input.KEY_D))
{
screenX = screenX - 3.3f;
plat1X = plat1X - 3.3f;
plat2X = plat2X - 3.3f;
plat3X = plat3X - 3.3f;
plat4X = plat4X - 3.3f;
plat5X = plat5X - 3.3f;
plat6X = plat6X - 3.3f;
plat7X = plat7X - 3.3f;
plat8X = plat8X - 3.3f;
playeranistart = runforani;
if(playplathit.intersects(plat1) || playplathit.intersects(plat2) || playplathit.intersects(plat3) || playplathit.intersects(plat4) || playplathit.intersects(plat5) || playplathit.intersects(plat6) || playplathit.intersects(plat7) || playplathit.intersects(plat8))
{
if(rightside)
{
plat1X = plat1X - 3.3f;
plat2X = plat2X - 3.3f;
plat3X = plat3X - 3.3f;
plat4X = plat4X - 3.3f;
plat5X = plat5X - 3.3f;
plat6X = plat6X - 3.3f;
plat7X = plat7X - 3.3f;
plat8X = plat8X - 3.3f;
screenX = screenX - 3.3f;
rightside = false;
}
else
{
plat1X = plat1X + 3.3f;
plat2X = plat2X + 3.3f;
plat3X = plat3X + 3.3f;
plat4X = plat4X + 3.3f;
plat5X = plat5X + 3.3f;
plat6X = plat6X + 3.3f;
plat7X = plat7X + 3.3f;
plat8X = plat8X + 3.3f;
screenX = screenX + 3.3f;
leftside = true;
}
}
}
if(input.isKeyDown(Input.KEY_A))
{
playeranistart = runbackani;
plat1X = plat1X + 3.3f;
plat2X = plat2X + 3.3f;
plat3X = plat3X + 3.3f;
plat4X = plat4X + 3.3f;
plat5X = plat5X + 3.3f;
plat6X = plat6X + 3.3f;
plat7X = plat7X + 3.3f;
plat8X = plat8X + 3.3f;
screenX = screenX + 3.3f;
if(playplathit.intersects(plat1) || playplathit.intersects(plat2) || playplathit.intersects(plat3) || playplathit.intersects(plat4) || playplathit.intersects(plat5) || playplathit.intersects(plat6) || playplathit.intersects(plat7) || playplathit.intersects(plat8))
{
if(leftside)
{
plat1X = plat1X + 3.3f;
plat2X = plat2X + 3.3f;
plat3X = plat3X + 3.3f;
plat4X = plat4X + 3.3f;
plat5X = plat5X + 3.3f;
plat6X = plat6X + 3.3f;
plat7X = plat7X + 3.3f;
plat8X = plat8X + 3.3f;
screenX = screenX + 3.3f;
leftside = false;
}
else
{
plat1X = plat1X - 3.3f;
plat2X = plat2X - 3.3f;
plat3X = plat3X - 3.3f;
plat4X = plat4X - 3.3f;
plat5X = plat5X - 3.3f;
plat6X = plat6X - 3.3f;
plat7X = plat7X - 3.3f;
plat8X = plat8X - 3.3f;
screenX = screenX - 3.3f;
rightside = true;
}
}
}
if(input.isKeyDown(Input.KEY_W))
{
if(jumping != true)
{
vertspd = 1.0f * (delta);
playerY = playerY - vertspd;
}
}
}
public int getID()
{
return 1;
}
}
Thank you for any help.