Question

This is the map: Blue squares' layer name : Background Cyan squares' layer name : Objects White square = player http://prntscr.com/2c2m5l (never mind the black space).

After watching a couple of tutorials I ended up coding this class:

public class LevelOne extends BasicGameState{

public int state;
boolean esc;
Input input;

private TiledMap map;

private int x,y;


public LevelOne(int state){
    this.state = state;
}

@Override
public void init(GameContainer arg0, StateBasedGame arg1)
        throws SlickException {
      esc = false;
      map = new TiledMap("res/map/map.tmx");

      x = 1;
      y = 1;
}

@Override
public void render(GameContainer arg0, StateBasedGame arg1, Graphics g)
        throws SlickException {
        map.render(0, 0);

        g.fillRect(x * 32, y * 32, 32, 32);

}

@Override
public void update(GameContainer gc, StateBasedGame sbg, int arg2)
        throws SlickException {
    input = gc.getInput();
    if(esc){
        Mouse.setGrabbed(false);

    }else{
        Mouse.setGrabbed(true);
    }
        if(input.isKeyPressed(Input.KEY_ESCAPE)){
            esc = true;
        }   
    if(esc){
        Mouse.setGrabbed(false);
            if(input.isKeyPressed(Input.KEY_R)){
                esc = false;
            }else if(input.isKeyPressed(Keyboard.KEY_Q)){
                System.exit(0);
            }else if(input.isKeyPressed(Keyboard.KEY_M)){
                esc = false;
                sbg.enterState(0);
                Mouse.setGrabbed(false);
            }
        }

    int objLayer = map.getLayerIndex("Objects");

    map.getTileId(0, 0, objLayer);


    if(input.isKeyPressed(Input.KEY_RIGHT)){
        if(map.getTileId(x+1, y, objLayer) == 0){
            x++;
        }
    }

    if(input.isKeyPressed(Input.KEY_LEFT)){
        if(map.getTileId(x-1, y, objLayer) == 0){
            x--;
        }
    }

    if(input.isKeyPressed(Input.KEY_UP)){
        if(map.getTileId(x, y-1, objLayer) == 0){
            y--;
        }
    }

    if(input.isKeyPressed(Input.KEY_DOWN)){
        if(map.getTileId(x, y+1, objLayer) == 0){
            y++;
        }
    }

        }

@Override
public int getID() {
    return state;
}

}

Collision detection is not working and I do not understand why I need these 2 lines:

int objLayer = map.getLayerIndex("Objects");

    map.getTileId(0, 0, objLayer);//this one in particular
Was it helpful?

Solution

map.getTileId(0, 0, objLayer);  //this one in particular

This line is not necessary. The method getTileID( int x , int y , int layerID ) will return the id of the tile placed at the position (x|y).

int objLayer = map.getLayerIndex("Objects");

This line will get you the layerID of the layer with the name "Objects". So basically you will have to have a look at your tile map file. In your tile map you can have multiple layers. Go to your map editor and search for the name of the layer where the tiles to collide with are placed. You can for example go and create an extra layer called "Objects" and then place tiles to collide with on that layer.

After you found that name you use getLayerIndex( String layerName ) to get the layerID of this layer as an integer.

Later on you can use that value in getTileID( int x , int y , int layerID ) to get an specific tile id on that layer. The last thing to take care about is, that you want to make sure wich tile has wich id.

One simple way to check your code would be to print the result of getTileID( int x , int y , int layerID ) in every if statement. With that you will see the object ids and you will see if it works.

Simply put that into every if statement, for example:

if(input.isKeyPressed(Input.KEY_LEFT)){
    System.out.println( "Try to walk left.Tile id left to the player is:"+
                                               map.getTileId(x-1, y, objLayer) );
    if(map.getTileId(x-1, y, objLayer) == 0){
        x--;
    }
}

So check for 1. the right layer name 2. the right tile id

With best regards, Chin

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top