Hello guys, I’m new to this forum and I need some help :/…
So my question is, is there any way I could make that one whole layer in tiled map editor is blocked…So when player tries to move he has to check if left tile right tile,upper tile and bottom tile is on the blocked layer if it is he isn’t able to move…Here’s my code:
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
int spritePosisionX=0;//map position, and also for moving player(changed only for map pos)
int spritePosisionY=0;//map position, and also for moving player(changed only for map pos)
float shiftX = spritePosisionX + 220;//player's starting position X(and for moving)
float shiftY = spritePosisionY + 200;//player's starting position Y(and for moving)
// private Music music; //music(that's for background)
private int coveringSpriteLayer;
private int backgroundLayer;
private int blockedLayer;
private int grassLayer;
private int waterLayer;
//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;//starting image
///////////////
}
public void render(GameContainer gc,StateBasedGame sbg,Graphics g) throws SlickException{
/////////////drawing a map!
//rendering background
tileMap1.render((int)spritePosisionX, (int)spritePosisionY,backgroundLayer);
//rendering blocked tiles
tileMap1.render((int)spritePosisionX, (int)spritePosisionY,blockedLayer);
//rendering grass tiles
tileMap1.render((int)spritePosisionX, (int)spritePosisionY,grassLayer);
//rendering water tiles
tileMap1.render((int)spritePosisionX, (int)spritePosisionY,waterLayer);
///////////drawing a player
Hero.draw(shiftX,shiftY);
/////////////
//rendering tiles which cover the sprite
tileMap1.render((int)spritePosisionX, (int)spritePosisionY,coveringSpriteLayer);
//////////////
///////drawing name
g.drawString(spriteName, shiftX-12, shiftY -20);
/////////
//////////////////////drawing player position(no point now)
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{
//loading layers from tmx file
coveringSpriteLayer = tileMap1.getLayerIndex("coveringUs");
backgroundLayer = tileMap1.getLayerIndex("background");
blockedLayer = tileMap1.getLayerIndex("block");
grassLayer = tileMap1.getLayerIndex("grass");
waterLayer = tileMap1.getLayerIndex("water");
//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)){
Hero=movingUp;
//spritePosisionY += delta * 0.09f;
shiftY -=delta * 0.09f;
}
//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;
shiftY +=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;
shiftX -=delta * 0.09f;
}
//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;
shiftX +=delta * 0.09f;
}
//mouse position
int MouseX= Mouse.getX();
int MouseY= Mouse.getY();
mouse = "Mouse Posison:" + MouseX + "," + MouseY;
}
public int getID(){
//state ID
return 1;
}
}