Hi! Sorry for spamming the forum recently with almost the same question but this time it's different. I've
followed freeaks tutorials
(
http://slick.cokeandcode.com/wiki/doku.php?id=freeaks_tutorials)
but when I add collision the game crashes when I try to move.
The code for the collision etc is here:
http://slick.cokeandcode.com/wiki/doku.php?id=04_-_slopes_and_collisions.
Here is the play.java code:
Code:
package javagame;
import org.newdawn.slick.*;
import org.newdawn.slick.geom.Polygon;
import org.newdawn.slick.geom.Shape;
import org.newdawn.slick.state.*;
import org.newdawn.slick.tiled.TiledMap;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.Animation;
import org.newdawn.slick.Input;
public class Play extends BasicGameState{
Animation angel, movingUp, movingDown, movingLeft, movingRight;
Image world;
int[] duration = {200,200};
float angelPositionX = 0;
float angelPositionY = 0;
float shiftX = 0;
float shiftY = 0;
private Polygon playerPoly;
private Polygon poly;
private TiledMap grassmap;
private Animation player;
public Play(int state){
}
public void init(GameContainer gc, StateBasedGame sbg) throws SlickException{
//world = new Image("res/world.png");
Image[] walkUp = {new Image("res/angelback.png"), new Image("res/angelback.png")};
Image[] walkDown = {new Image("res/Angel.png"), new Image("res/Angel.png")};
Image[] walkLeft = {new Image("res/angelLeft.png"), new Image("res/angelLeft.png")};
Image[] walkRight = {new Image("res/angelRight.png"), new Image("res/angelRight.png")};
grassmap = new TiledMap("res/grassmap.tmx");
movingUp = new Animation(walkUp, duration, true);
movingDown = new Animation(walkDown, duration, true);
movingLeft = new Animation(walkLeft, duration, true);
movingRight = new Animation(walkRight, duration, true);
angel = movingDown;
playerPoly = new Polygon(new float[]{
angelPositionX,angelPositionY,
angelPositionX+32,angelPositionY,
angelPositionX+32,angelPositionY+32,
angelPositionX,angelPositionY+32
});
}
public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException{
//X, Y, Scale float
//world.draw(angelPositionX, angelPositionY);
grassmap.render(0, 0);
angel.draw(angelPositionX + shiftX, angelPositionY + shiftY);
g.drawString("X: "+angelPositionX+"\nY: "+angelPositionY, 10, 25);
//if((angelPositionX>-100 && angelPositionY>-100) && (angelPositionX<100 && angelPositionY<100)){
//g.drawString("You should visit the time temple!", 400, 300);
}
public void update(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException{
if (gc.getInput().isKeyDown(Input.KEY_LEFT)) {
angelPositionX--;
angel = movingLeft;
}
if (gc.getInput().isKeyDown(Input.KEY_RIGHT)) {angelPositionX++;
angel = movingRight;
if (entityCollisionWith()){
angelPositionX--;
}
}
if (gc.getInput().isKeyDown(Input.KEY_UP)) {angelPositionY--;
angel = movingUp;
if (entityCollisionWith()){
angelPositionY++;
}
}
if (gc.getInput().isKeyDown(Input.KEY_DOWN)) {angelPositionY++;
angel = movingDown;
if (entityCollisionWith()){
angelPositionY--;
}
}
Input input = gc.getInput();
if(input.isKeyDown(Input.KEY_ESCAPE)){
sbg.enterState(3);
}
}
//Collision
public boolean entityCollisionWith() throws SlickException {
for (int i = 0; i < BlockMap.entities.size(); i++) {
Block entity1 = (Block) BlockMap.entities.get(i);
if (poly.intersects(entity1.poly)) {
return true;
}
}
return false;
}
public int getID(){
return 1;
}
}
The Block.java and BlockMap.java source codes can be found at
http://slick.cokeandcode.com/wiki/doku.php?id=04_-_slopes_and_collisionsIf you help me with this I'll do something awesome for you. I'm not quite sure what yet but it will be awesome! Thanks!

**EDIT**
I changed the movement to this:
Code:
public void update(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException{
Input input = gc.getInput();
if(input.isKeyDown(Input.KEY_LEFT)){
angel = movingLeft;
angelPositionX -= delta * .3f;
}
if(input.isKeyDown(Input.KEY_RIGHT)){
angel = movingRight;
angelPositionX += delta * .3f;
}
if(input.isKeyDown(Input.KEY_UP)){
angel = movingUp;
angelPositionY -= delta * .3f;
}
if(input.isKeyDown(Input.KEY_DOWN)){
angel = movingDown;
angelPositionY += delta * .3f;
}
Thanks to AtomizerZero for helping me although you didn't really solve it so I guess I'll just have to take myself out for dinner tonight! I hope we'll go somewhere fancy!
