0

我是 Java 游戏开发以及 Slick 2D 的新手,我正在制作一款像马里奥或索尼克这样的平台游戏。无论如何,我在碰撞检测方面遇到了麻烦。我已经设置了代码,但我的播放器一直在浏览地图。我正在使用平铺地图编辑器制作地图。这是运动和碰撞检测的代码

变量

   private float marioX = 12;
   private float marioY = 590;
   boolean jumping = false;        
   float verticalSpeed = 0.0f;     
   private Polygon playerPoly;
   public BlockMap map2;


Input input = gc.getInput();

if (input.isKeyDown(Input.KEY_LEFT)){
    mario = marioLeft;
    marioX-=5;
    playerPoly.setX(marioX);
    if (entityCollisionWith()){
        marioX+=5;
        playerPoly.setX(marioX);
    }

}   
if (input.isKeyDown(Input.KEY_RIGHT)){
    mario = marioRight;
    marioX+=5;
    playerPoly.setX(marioX);
    if (entityCollisionWith()){
        marioX-=5;
        playerPoly.setX(marioX);
    }
}


if (input.isKeyDown(Input.KEY_SPACE) && !jumping) {
    mario = marioJumpRight;
    verticalSpeed = -.4f * delta;
    jumping = true;
}
if (jumping){
    verticalSpeed += .01f * delta;
}

marioY += verticalSpeed;
playerPoly.setY(marioY);

if (entityCollisionWith()){
    jumping = false;
    verticalSpeed = 0;
}
entityCollisionWith()

public boolean entityCollisionWith() throws SlickException {
for (int i = 0; i < BlockMap.entities.size(); i++) {
    Blocks entity1 = (Blocks) BlockMap.entities.get(i);
    if (playerPoly.intersects(entity1.poly)) {
        return true;
    }       
}       
return false;
}

块映射类

import java.util.ArrayList;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.tiled.TiledMap;

public class BlockMap {
public static TiledMap tmap;
public static int mapWidth;
public static int mapHeight;
private int square[] = {1,1,15,1,15,15,1,15}; //square shaped tile
public static ArrayList<Object> entities;

public BlockMap(String ref) throws SlickException {
entities = new ArrayList<Object>();
tmap = new TiledMap(ref, "res");
mapWidth = tmap.getWidth() * tmap.getTileWidth();
mapHeight = tmap.getHeight() * tmap.getTileHeight();

for (int x = 0; x < tmap.getWidth(); x++) {
    for (int y = 0; y < tmap.getHeight(); y++) {
        int tileID = tmap.getTileId(x, y, 0);
        if (tileID == 1) {
            entities.add(
                                new Blocks(x * 16, y * 16, square, "square")
                                );
        }
    }
   }
 }
}

块类

import org.newdawn.slick.Graphics;
import org.newdawn.slick.geom.Polygon;

public class Blocks  {
public Polygon poly;
public Blocks(int x, int y, int test[],String type) {
poly = new Polygon(new float[]{
        x+test[0], y+test[1],
        x+test[2], y+test[3],
        x+test[4], y+test[5],
        x+test[6], y+test[7],
 });   
 }

public void update(int delta) {
}

public void draw(Graphics g) {
   g.draw(poly);
 }
}
4

1 回答 1

1

您确定要从正确的图层中获取图块吗?与您的代码一样,您从图层 ID '0' 获取图块:

for (int x = 0; x < tmap.getWidth(); x++) {
    for (int y = 0; y < tmap.getHeight(); y++) {
        int tileID = tmap.getTileId(x, y, 0);
        if (tileID == 1) {
            entities.add(
               new Blocks(x * 16, y * 16, square, "square")
               );
        }
    }
}

您可以尝试更改此行:

int tileID = tmap.getTileId(x, y, 0);

...对此:

int layerID = tmap.getLayerIndex( "CollisionLayer" ); // Or w/e your layer is called
int tileID = tmap.getTileId( x, y, layerID );

希望这可以帮助!

于 2013-08-28T18:03:05.633 回答