Hyp9r
Hyp9r

Reputation: 38

Java slick2D, collision with Tiled(map editor)

So my question is, I made a layer in Tiled and called it block.So is there any way I could make that whenever player tries to walk on block layer he can't...How do I do that? Here's my code: few screen shots: http://prntscr.com/3vya2d (this is how it looks in tiled) in game: http://prntscr.com/3vya9a

    package javagame;
    import org.lwjgl.input.Mouse;
    import org.newdawn.slick.*;
    import org.newdawn.slick.state.*;
    import org.newdawn.slick.tiled.TiledMap;
    public class Play extends BasicGameState{
Animation Hero, movingUp, movingDown, movingLeft, movingRight,standingfront;
private String spriteName = "Heroj";//string above the player
private TiledMap tileMap1;//tiled map
int[] duration = {100,100,100,100};//duration for how long is animation going to                              last
public String mouse = "1";-just for mouse position variable
    float spritePosisionX=0;-map position, and also for moving player
float spritePosisionY=0;-map position, and also for moving player
float shiftX = spritePosisionX + 220;-player's starting position X
float shiftY = spritePosisionY + 200;-player's starting position Y
    //private Music music; -music(that's for background)
private int coveringSprite;-
private int background;-
private int blocked;-
private int grass;-
private int water;-
//constructor
public Play(int state){

    }

public void init(GameContainer gc,StateBasedGame sbg)throws SlickException{


    //music
    /*music = new Music("sounds/PalletTownMix.wav");
    music.setVolume(0.5f);
    music.loop();*/
    ///////////////


    //loading the map
    tileMap1 = new TiledMap("maps/map3.tmx");-
    //////////////////////////////

    //making the animations
    Image[] walkUp ={new Image("res/heroback1.PNG"),new          Image("res/heroback2.PNG"),new Image("res/heroback1.PNG"),new Image("res/heroback3.PNG")};
  Image[] standingf = {new Image("res/herofront1.PNG"),new Image("res/herofront1.PNG"),new Image("res/herofront1.PNG"),new Image("res/herofront1.PNG")};
  Image[] walkDown = {new Image("res/herofront1.PNG"), new Image("res/herofront2.PNG"), new Image("res/herofront1.PNG"), new Image("res/herofront3.PNG")};
  Image[] walkRight ={new Image("res/heroright1.PNG"),new Image("res/heroright2.PNG"),new Image("res/heroright1.PNG"),new Image("res/heroright3.PNG")};
  Image[] walkLeft ={new Image("res/heroleft1.PNG"),new Image("res/heroleft2.PNG"),new Image("res/heroleft1.PNG"),new Image("res/heroleft3.PNG")};


  //animation for walking up
  movingUp = new Animation(walkUp,duration);
  ////////////////////

  //animation for walking down
  movingDown = new Animation(walkDown, duration);
  ////////////////////

  //animation for standing(none)
  standingfront = new Animation(standingf, duration);
  ////////////////////

  //animation for right walking
  movingRight = new Animation(walkRight,duration);
  ////////////////////

  //animation for left walking
  movingLeft = new Animation(walkLeft,duration);
  ////////////////////

  //sprite's starting animation(he doesn't move,front sprite
  Hero = standingfront;
  ///////////////
     }

public void render(GameContainer gc,StateBasedGame sbg,Graphics g) throws SlickException{
    /////////////drawing a map!
    //rendering background
    tileMap1.render((int)spritePosisionX, (int)spritePosisionY,background);
    //rendering blocked tiles
    tileMap1.render((int)spritePosisionX, (int)spritePosisionY,blocked);
    //rendering grass tiles
    tileMap1.render((int)spritePosisionX, (int)spritePosisionY,grass);
    //rendering water tiles
    tileMap1.render((int)spritePosisionX, (int)spritePosisionY,water);
            ///////////drawing a player
    Hero.draw(shiftX,shiftY);
    /////////////
    //rendering tiles which cover the sprite
    tileMap1.render((int)spritePosisionX, (int)spritePosisionY,coveringSprite);
    //////////////
    ///////drawing name
    g.drawString(spriteName, shiftX-12, shiftY -20);
    /////////

    //////////////////////drawing player position
    g.drawString("PlayerPos: "+ spritePosisionX +"," + spritePosisionY, 340, 10);
   ////////////////////////////////////////////////
    //drawing mouse position!
    g.drawString(mouse, 440, 30);
    //////////////////////////////
}

public void update(GameContainer gc,StateBasedGame sbg,int delta)throws SlickException{
    coveringSprite = tileMap1.getLayerIndex("coveringUs");
    background = tileMap1.getLayerIndex("background");
    blocked = tileMap1.getLayerIndex("block");
    grass = tileMap1.getLayerIndex("grass");
    water = tileMap1.getLayerIndex("water");
    tileMap1.getTileId(1,1,blocked);
    //key,mouse input
    Input input = gc.getInput();


    //up
    if(input.isKeyDown(Input.KEY_W) && input.isKeyDown(Input.KEY_A) ||input.isKeyDown(Input.KEY_D)){
    }
    else if(input.isKeyDown(Input.KEY_W)){
              spritePosisionY += delta * 0.09f;
                Hero=movingUp;
          }     
    //down
    if(input.isKeyDown(Input.KEY_S) && input.isKeyDown(Input.KEY_D) ||input.isKeyDown(Input.KEY_A)){
    }
    else if (input.isKeyDown(Input.KEY_S)){
    Hero=movingDown;
    spritePosisionY -= delta * 0.09f;
    }


    //left
    if(input.isKeyDown(Input.KEY_A) && input.isKeyDown(Input.KEY_S) ||input.isKeyDown(Input.KEY_W)){
    }
    else if(input.isKeyDown(Input.KEY_A)){
        spritePosisionX += delta * 0.09f;
        Hero=movingLeft;
    }

    //right
    if(input.isKeyDown(Input.KEY_D) && input.isKeyDown(Input.KEY_S) ||input.isKeyDown(Input.KEY_W)){
    }
    else if(input.isKeyDown(Input.KEY_D)){
        spritePosisionX -= delta * 0.09f;
            Hero=movingRight;
    }


    //mouse position
    int MouseX= Mouse.getX();
    int MouseY= Mouse.getY();
    mouse = "Mouse Posison:" + MouseX + "," + MouseY;
}

public int getID(){
    //state ID
    return 1;
}

}

Upvotes: 1

Views: 1279

Answers (2)

XPY64K
XPY64K

Reputation: 34

A better way to do this is by using tile properties.You do this by opening your tileset, selecting all tiles and click on the add property button. There you choose boolean and name it "solid". You can check the tiles property with this snippet:

    List<Rectangle> collisionList = new ArrayList<Rectangle>();
    for(int i=0;i<tiledMap.getWidth();i++){
        for(int j=0;j<tiledMap.getHeight();j++){
            for(int a=0;a<getLayerCount();a++){
                String property = tiledMap.getTileProperty(tiledMap.getTileId(i, j, a), "solid", "null");
                if(property.equals("null")){
                    throw new SlickException("Failed to get tileproperty!");
                }else if(property.equals("true")){
                    collisionList.add(new Rectangle(i*getTileWidth(),j*getTileHeight(),getTileWidth(),getTileHeight()));
                }
            }
        }
    }

then you can implement the player movment like this:

        Rectangle collision = new Rectangle(x,y,playerSizeX,playerSizeY);
    float deltaX = x;
    float deltaY = y;
    if (input.isKeyDown(Input.KEY_LEFT)|input.isKeyDown(Input.KEY_A)){
        sprite = left;
        deltaX -= delta * 0.1f;
    }else if (input.isKeyDown(Input.KEY_RIGHT)|input.isKeyDown(Input.KEY_D)){
        sprite = right;
        deltaX += delta * 0.1f;
    }
    if (input.isKeyDown(Input.KEY_UP)|input.isKeyDown(Input.KEY_W)){
        sprite = up;
        deltaY -= delta * 0.1f;
    }else if (input.isKeyDown(Input.KEY_DOWN)|input.isKeyDown(Input.KEY_S)){
        sprite = down;
        deltaY += delta * 0.1f;
    }
    float clipX = x;

    collision.setLocation(deltaX+offsetX,y+offsetY);
    if(!intersects(collision)){
        sprite.update(delta);
        x = deltaX;
    }
    collision.setLocation(clipX+offsetX,deltaY+offsetY);
    if(!intersects(collision)){
        sprite.update(delta);
        y = deltaY;
    }

Here the Method intersects():

public boolean intersects(Rectangle player) {
    for(int i=0; i<collisionList.size(); i++){
        if(collisionList.get(i).intersects(player)){
            return true;
        }
    }
    return false;
}

Upvotes: 1

Stefnotch
Stefnotch

Reputation: 695

You could try making a "rectangle" object for every tile (Or every tile that is solid)

Rectangle tile = new Rectangle(x,y,width,height);  

Then set up a rectangle object for your character.

Rectangle character = new Rectangle(x,y,width,height); 

Then, use a "for loop" to run through the tile map and check for a collision. e.g.

for(int i=0; i<tileMap1.size(); i++){
    if(character.intersects(tileMap1[i].tile){
    //Do something..
    }
}

Upvotes: 0

Related Questions