我是Java Game Development和Slick 2D的新手,并且正在开发像Mario或Sonic这样的平台游戏。无论如何,我在碰撞检测方面遇到了麻烦。我已经设置了代码,但是我的玩家不断浏览地图。我正在使用“平铺地图”编辑器制作地图。这是移动和碰撞检测的代码

变数

   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;
}


BlockMap类

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);
 }
}

最佳答案

您确定要从正确的图层上取砖吗?就像在您的代码中一样,您将从图层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 );


希望这可以帮助!

08-05 05:08