我很开心构建2D平铺游戏(没有libgdx的Slick2D),并且我在超过15个小时的时间内处理碰撞。我认为是时候提问了。我搜索了很多,但没有真正回应我搜索的内容。Java - 二维平铺游戏中的检测碰撞
所以,这是个什么样子:2D game capture(深色瓷砖都被玩家不能行走)和Another 2D game capture
矩形是被碰撞可能发生。在那里,当它们碰撞时,它仍然移动到足以锁定在瓦片中。 (不能再做什么)
所以,我的类被输入处理程序,更新和绘图调用。论文三由“玩”类。谁只是更新的东西。 (Player.MoveUp(),Player.draw,...)
的 “WorldManager” 是其中瓦片被管理(他们的只有4对GetType()是查找是否阻止(0有效,1封锁)
“LevelGeneration”是瓦片的位置,它的ID和 映射(用一个int [] [])(他确定'它创建带有ID的地图,以及何时我
显示然后他们都很好)AssetManager是我在哪里削减精灵表转换th en BufferedImage
tilesize是50,玩家是41x36。
我有一个问题,因为我想有mouvements的自由(4条边使8个mouvements侧)和定位点似乎是在upperleft侧
因此,这里是我的类(没有进口和包):
public class Player {
public static double SPEED;
public static int WIDTH, HEIGHT;
public static float XPos, YPos;
public static float destXPos, destYPos;
public static BufferedImage presentImage;
public static int[] position;
public static boolean moving;
public static boolean movementlock;
public static int side; // 0 up 1 right 2 down 3 left
public static Rectangle p = null;
public static Rectangle e = null;
private static boolean mouvementlock;
private static boolean collision;
public static void CreateHero(){
// Valeurs de test
position = new int[2];
SPEED = 1;
XPos = destXPos = 100;
YPos = destYPos = 100;
presentImage = AssetManager.Player[0][0];
moving = false;
movementlock = false;
collision = false;
position[0] = (int) (XPos/WorldManager.TileSize);
position[1] = (int) (YPos/WorldManager.TileSize);
}
public static void MoveUp(){
side = 0;
destYPos -= SPEED;
moving = ValidateNextPosition();
}
public static void MoveRight(){
side = 1;
destXPos += SPEED;
moving = ValidateNextPosition();
}
public static void MoveDown(){
side = 2;
destYPos += SPEED;
moving = ValidateNextPosition();
}
public static void MoveLeft(){
side = 3;
destXPos -= SPEED;
moving = ValidateNextPosition();
}
static void updateMovement(){
if(moving == true && mouvementlock == false){
mouvementlock = true;
System.out.println("Mouvement");
System.out.println("X : " + XPos);
System.out.println("Y : " + YPos);
XPos = destXPos;
YPos = destYPos;
System.out.println("Xdest : " + destXPos);
System.out.println("Ydest : " + destYPos);
System.out.println("" + moving);
moving = false;
mouvementlock = false;}
else{
destXPos = XPos;
destYPos = YPos;
}
}
public static boolean ValidateNextPosition(){
if(moving) return false;
if(collision) return false;
p = new Rectangle((int) XPos, (int) YPos, presentImage.getWidth(null),presentImage.getHeight(null));
int tileid = 0;
boolean validate = true;
int destpos[] = new int[2];
double TileCollision[] = new double[2];
destpos[0] = (int) destXPos/WorldManager.TileSize;
destpos[1] = (int) destYPos/WorldManager.TileSize;
TileCollision[0] = 0;
TileCollision[1] = 0;
if(side == 0 && collision != true){
if(destpos[1] - 1 > 0){
tileid = LevelGenerator.getIdmap()[destpos[0]][destpos[1] - 1];
if(destYPos + 1 < 0 || WorldManager.Tuiles[tileid].getType() == 1){
destpos[1] -= 1;collision = true;}}
}
if(side == 1 && collision != true){
tileid = LevelGenerator.getIdmap()[destpos[0] + 1][destpos[1]];
if(destXPos + 1 < 0 || WorldManager.Tuiles[tileid].getType() == 1){
destpos[0] += 1;collision = true;}
}
if(side == 2 && collision != true){
tileid = LevelGenerator.getIdmap()[destpos[0]][destpos[1] + 1];
if(destYPos - 1 < 0 || WorldManager.Tuiles[tileid].getType() == 1){
destpos[1] += 1;collision = true;}
}
if(side == 3 && collision != true){
tileid = LevelGenerator.getIdmap()[destpos[0]][destpos[1]];
if(destXPos - 1 < 0 || WorldManager.Tuiles[tileid].getType() == 1){
/*destpos[0] -= 1*/;collision = true;}
}
if (collision == true){
TileCollision[0] = LevelGenerator.getPosmap()[destpos[0]][destpos[1]][0];
TileCollision[1] = LevelGenerator.getPosmap()[destpos[0]][destpos[1]][1];
e = new Rectangle((int) TileCollision[0], (int) TileCollision[1],WorldManager.TileSize,WorldManager.TileSize);}
if(e != null && p.intersects(e)){
validate = false;
System.out.println("collision");}
return validate;
}
public static void update() {
updateMovement();
}
public static void draw(Graphics2D g) {
g.drawImage(presentImage,(int) XPos,(int) YPos,presentImage.getWidth(null),presentImage.getWidth(null),null);
if(p != null){
g.setColor(Color.BLACK);
g.drawRect(p.x, p.y, p.width, p.height);}
if(e != null){
g.setColor(Color.BLACK);
g.drawRect(e.x, e.y, e.width, e.height);}
}
}
要恢复,
- 碰撞发生为时已晚和玩家卡在被阻挡的区块。
- 有时它会通过它,因为(我想)左上角没有碰到该块。
- 它可以走出去的地图没有问题,然后坠毁,但一切我试图保持崩溃游戏
非常感谢你,有一个愉快的一天
Hell'no
(PS:英语是我的第三语言,所以问我你是否完全不懂)
你为什么要我们修复你的游戏?你为什么不选择一个问题,并专注于它它是一个混乱 – gpasch