Information:
The tiled map and player : http://prntscr.com/2c445u . I tried to make some kind of pixel-precise movement
It is weird because I can achieve stuff like this sometimes : http://prntscr.com/2c44px
I can not figure out how to prevent these bugs…(It needs to works with more maps too…)I believe that the whole issue is caused by the way I calculate tileX and tileY (player’s positions @ grid. Is there a better way?)
This thing(in pic) only happens when I try to move left or right and I am in the middle of 2 tiles. Is there any way to prevent that?
(I am not a noob with java. Its just the fact that I am now taking my first steps in game-dev.)
Here is the full code:
//packet
//imports
public class LevelOne extends BasicGameState{
public int state;
boolean esc;
Input input;
private TiledMap map;
private float x,y,vel;
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 = 2 * 32;
y = 1 * 32;
vel = .1f;
}
@Override
public void render(GameContainer arg0, StateBasedGame arg1, Graphics g)
throws SlickException {
map.render(0, 0);
g.fillRect(x, y, 32, 32);
}
@Override
public void update(GameContainer gc, StateBasedGame sbg, int delta)
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");
int tileX = (int)(x + 1)/ 32;
int tileY = (int)(y + 1)/ 32;
System.out.println(tileX +" "+ tileY);
//TODO -- FIX COLLISIONS
if(input.isKeyDown(Input.KEY_RIGHT)){
if(map.getTileId(tileX+1, tileY, objLayer) == 0){
x+= vel * delta;
}
}
if(input.isKeyDown(Input.KEY_LEFT)){
tileX++;
if(map.getTileId(tileX-1, tileY, objLayer) == 0){
x-= vel * delta;
}
tileX--;
}
if(input.isKeyDown(Input.KEY_UP)){
tileY++;
if(map.getTileId(tileX, tileY-1, objLayer) == 0){
y-= vel * delta;
}
tileY--;
}
if(input.isKeyDown(Input.KEY_DOWN)){
if(map.getTileId(tileX, tileY+1, objLayer) == 0){
y += vel * delta;
}
}
}
@Override
public int getID() {
return state;
}
}
Thanks in advance