Slick Forums

Discuss the Slick 2D Library
It is currently Tue May 21, 2013 4:47 pm

All times are UTC




Post new topic Reply to topic  [ 7 posts ] 
Author Message
PostPosted: Wed Jul 11, 2012 9:09 am 
Offline

Joined: Thu Jun 21, 2012 3:27 pm
Posts: 27
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_collisions

If 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! :D

**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! :D

_________________
http://ReinventGaming.com/


Last edited by leosiden on Wed Jul 11, 2012 9:45 am, edited 1 time in total.

Top
 Profile  
 
PostPosted: Wed Jul 11, 2012 9:30 am 
Offline

Joined: Mon May 07, 2012 11:36 pm
Posts: 93
Due to the formatting being pretty messy in your update method, it's hard to really tell whats going on.. but I did notice that your not checking for collisions when you press Left.

EDIT: Actually, there's a lot wrong with your update method. For example:
Code:
     
Input input = gc.getInput();

if(input.isKeyDown(Input.KEY_ESCAPE))

if (gc.getInput().isKeyDown(Input.KEY_DOWN))



You should put that "Input input = gc.getInput();" at the top of the update method, and change all "gc.getInput()." calls to "input".


Top
 Profile  
 
PostPosted: Wed Jul 11, 2012 9:38 am 
Offline

Joined: Thu Jun 21, 2012 3:27 pm
Posts: 27
AtomizerZero wrote:
Due to the formatting being pretty messy in your update method, it's hard to really tell whats going on.. but I did notice that your not checking for collisions when you press Left.

EDIT: Actually, there's a lot wrong with your update method. For example:
Code:
     
Input input = gc.getInput();

if(input.isKeyDown(Input.KEY_ESCAPE))

if (gc.getInput().isKeyDown(Input.KEY_DOWN))



You should put that "Input input = gc.getInput();" at the top of the update method, and change all "gc.getInput()." calls to "input".


it still crashes when I try to move :( Also sorry for the messy code :P

_________________
http://ReinventGaming.com/


Top
 Profile  
 
PostPosted: Wed Jul 11, 2012 9:40 am 
Offline

Joined: Mon May 07, 2012 11:36 pm
Posts: 93
Could you make a zip of your project for me? i'll take a look at it for you. (too lazy to copy your code and build a project lol)


Top
 Profile  
 
PostPosted: Wed Jul 11, 2012 9:45 am 
Offline

Joined: Thu Jun 21, 2012 3:27 pm
Posts: 27
Yay I solved it! Read the "**EDIT**" thingy at the bottom if you want to know how.

_________________
http://ReinventGaming.com/


Top
 Profile  
 
PostPosted: Wed Jul 11, 2012 9:50 am 
Offline

Joined: Mon May 07, 2012 11:36 pm
Posts: 93
Glad you got it working, but, surely your collisions don't work now though, right?


Top
 Profile  
 
PostPosted: Wed Jul 11, 2012 10:54 am 
Offline

Joined: Thu Jun 21, 2012 3:27 pm
Posts: 27
AtomizerZero wrote:
Glad you got it working, but, surely your collisions don't work now though, right?

Nope. Working on it.

_________________
http://ReinventGaming.com/


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 7 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group