所以我的问题是,我在Tiled中创建了一个图层并将其命名为block.So是否有任何方法可以让每当玩家尝试在块图层上行走时他无法做到...我该怎么做?这是我的代码: 几个屏幕截图:http://prntscr.com/3vya2d(这是它在平铺中的样子) 在游戏中: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;
}
}
答案 0 :(得分:1)
更好的方法是使用图块属性。您可以通过打开图块集,选择所有图块并单击添加属性按钮来执行此操作。在那里你选择布尔并命名为#34; solid"。您可以使用以下代码段检查tiles属性:
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()));
}
}
}
}
然后你可以像这样实现播放器动作:
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;
}
这里方法与():
相交public boolean intersects(Rectangle player) {
for(int i=0; i<collisionList.size(); i++){
if(collisionList.get(i).intersects(player)){
return true;
}
}
return false;
}
答案 1 :(得分:0)
您可以尝试为每个图块(或每个固体图块)制作一个“矩形”对象
Rectangle tile = new Rectangle(x,y,width,height);
然后为角色设置一个矩形对象。
Rectangle character = new Rectangle(x,y,width,height);
然后,使用“for循环”运行切片贴图并检查碰撞。 e.g。
for(int i=0; i<tileMap1.size(); i++){
if(character.intersects(tileMap1[i].tile){
//Do something..
}
}