Java 游戏开发中影响实体移动的错误
Bug Affecting Entity Movement in Java Game Development
我正在学习 Youtube 上关于如何在 Java 中制作 2D 风格游戏的教程(Click here 对于视频 1,尽管我目前正在制作视频 #20)
我最近尝试添加导致玩家 teleport/heal/take 伤害的事件板块。我在排除故障时不再有传送选项,但基本上当我开始游戏时发生的事情是我的玩家在它应该出现的地方生成,但是当我按下任何方向键时,我会立即传送到左上角的方块并且我没有知道为什么。
我已将其缩小到我的 checkEvent 方法,我将在下面 post。
如果我可以 post 更多代码,请告诉我,我还有一个 KeyHandler、Entity、Player 和 Event Rectangle 类,但在故障排除中,直到我拿走了下面的 checkEvent 方法,我又可以移动了。
package main;
public class EventHandler {
GamePanel gp;
EventRect eventRect[][];
int previousEventX, previousEventY;
boolean canTouchEvent = true;
public EventHandler(GamePanel gp) {
this.gp = gp;
eventRect = new EventRect[gp.maxWorldCol][gp.maxWorldRow];
int col = 0;
int row = 0;
while (col < gp.maxWorldCol && row < gp.maxWorldRow) {
eventRect[col][row] = new EventRect();
eventRect[col][row].x = 23;
eventRect[col][row].y = 23;
eventRect[col][row].width = 2;
eventRect[col][row].height = 2;
eventRect[col][row].eventRectDefaultX = eventRect[col][row].x;
eventRect[col][row].eventRectDefaultY = eventRect[col][row].y;
col++;
if (col == gp.maxWorldCol) {
col = 0;
row++;
}
}
}
public void checkEvent() {
//check if character is one tile away from last event
int xDistance = Math.abs(gp.player.worldX = previousEventX);
int yDistance = Math.abs(gp.player.worldY = previousEventY);
int distance = Math.max(xDistance, yDistance);
if (distance > gp.tileSize) {
canTouchEvent = true;
}
if(canTouchEvent == true) {
if (hit(27, 16, "any") == true) {damagePit(27, 16, gp.dialogueState);}
if (hit(27, 18, "any") == true) {healingPool(27, 18, gp.dialogueState);}
}
}
public boolean hit(int col, int row, String reqDirection) {
boolean hit = false;
gp.player.solidArea.x = gp.player.worldX + gp.player.solidArea.x;
gp.player.solidArea.y = gp.player.worldY + gp.player.solidArea.y;
eventRect[col][row].x = col*gp.tileSize + eventRect[col][row].x;
eventRect[col][row].y = row*gp.tileSize + eventRect[col][row].y;
if (gp.player.solidArea.intersects(eventRect[col][row]) && eventRect[col][row].eventDone == false) {
if (gp.player.direction.contentEquals(reqDirection) || reqDirection.contentEquals("any")) {
hit = true;
previousEventX = gp.player.worldX;
previousEventY = gp.player.worldY;
}
}
gp.player.solidArea.x = gp.player.solidAreaDefaultX;
gp.player.solidArea.y = gp.player.solidAreaDefaultY;
eventRect[col][row].x = eventRect[col][row].eventRectDefaultX;
eventRect[col][row].y = eventRect[col][row].eventRectDefaultY;
return hit;
}
/*public void teleport(int col, int row, int gameState) {
gp.gameState = gp.gameState;
gp.ui.currentDialogue = "NYOOM";
gp.player.worldX = gp.tileSize * 17;
gp.player.worldY = gp.tileSize * 20;
canTouchEvent = false;
}
*/
public void damagePit(int col, int row, int gameState) {
gp.gameState = gameState;
gp.ui.currentDialogue = "MWAHA! You dun messed up";
gp.player.life -= 1;
//eventRect[col][row].eventDone = true;
canTouchEvent = true;
}
public void healingPool(int col, int row, int gameState) {
if (gp.keyH.enterPressed == true) {
gp.gameState = gameState;
gp.ui.currentDialogue = "Ahh, moistness";
gp.player.life = gp.player.maxLife;
}
}
}
这是我第一次posting,所以如果有我不遵守的礼节,请告诉我:)
你的checkEvent()
方法是错误的:
public void checkEvent() {
//check if character is one tile away from last event
int xDistance = Math.abs(gp.player.worldX = previousEventX);
int yDistance = Math.abs(gp.player.worldY = previousEventY);
int distance = Math.max(xDistance, yDistance);
if (distance > gp.tileSize) {
canTouchEvent = true;
}
if(canTouchEvent == true) {
if (hit(27, 16, "any") == true) {damagePit(27, 16, gp.dialogueState);}
if (hit(27, 18, "any") == true) {healingPool(27, 18, gp.dialogueState);}
}
}
在行 int xDistance = Math.abs(gp.player.worldX = previousEventX);
中,您将 previousEventX
的当前值(即 0)分配给 gp.player.worldX
(将玩家传送到 X 坐标 0)并设置xDistance
到 0(因为你计算 Math.abs(0)
)。
yDistance
的下一行类似。
将这两行更改为
int xDistance = Math.abs(gp.player.worldX - previousEventX);
int yDistance = Math.abs(gp.player.worldY - previousEventY);
我正在学习 Youtube 上关于如何在 Java 中制作 2D 风格游戏的教程(Click here 对于视频 1,尽管我目前正在制作视频 #20)
我最近尝试添加导致玩家 teleport/heal/take 伤害的事件板块。我在排除故障时不再有传送选项,但基本上当我开始游戏时发生的事情是我的玩家在它应该出现的地方生成,但是当我按下任何方向键时,我会立即传送到左上角的方块并且我没有知道为什么。
我已将其缩小到我的 checkEvent 方法,我将在下面 post。
如果我可以 post 更多代码,请告诉我,我还有一个 KeyHandler、Entity、Player 和 Event Rectangle 类,但在故障排除中,直到我拿走了下面的 checkEvent 方法,我又可以移动了。
package main;
public class EventHandler {
GamePanel gp;
EventRect eventRect[][];
int previousEventX, previousEventY;
boolean canTouchEvent = true;
public EventHandler(GamePanel gp) {
this.gp = gp;
eventRect = new EventRect[gp.maxWorldCol][gp.maxWorldRow];
int col = 0;
int row = 0;
while (col < gp.maxWorldCol && row < gp.maxWorldRow) {
eventRect[col][row] = new EventRect();
eventRect[col][row].x = 23;
eventRect[col][row].y = 23;
eventRect[col][row].width = 2;
eventRect[col][row].height = 2;
eventRect[col][row].eventRectDefaultX = eventRect[col][row].x;
eventRect[col][row].eventRectDefaultY = eventRect[col][row].y;
col++;
if (col == gp.maxWorldCol) {
col = 0;
row++;
}
}
}
public void checkEvent() {
//check if character is one tile away from last event
int xDistance = Math.abs(gp.player.worldX = previousEventX);
int yDistance = Math.abs(gp.player.worldY = previousEventY);
int distance = Math.max(xDistance, yDistance);
if (distance > gp.tileSize) {
canTouchEvent = true;
}
if(canTouchEvent == true) {
if (hit(27, 16, "any") == true) {damagePit(27, 16, gp.dialogueState);}
if (hit(27, 18, "any") == true) {healingPool(27, 18, gp.dialogueState);}
}
}
public boolean hit(int col, int row, String reqDirection) {
boolean hit = false;
gp.player.solidArea.x = gp.player.worldX + gp.player.solidArea.x;
gp.player.solidArea.y = gp.player.worldY + gp.player.solidArea.y;
eventRect[col][row].x = col*gp.tileSize + eventRect[col][row].x;
eventRect[col][row].y = row*gp.tileSize + eventRect[col][row].y;
if (gp.player.solidArea.intersects(eventRect[col][row]) && eventRect[col][row].eventDone == false) {
if (gp.player.direction.contentEquals(reqDirection) || reqDirection.contentEquals("any")) {
hit = true;
previousEventX = gp.player.worldX;
previousEventY = gp.player.worldY;
}
}
gp.player.solidArea.x = gp.player.solidAreaDefaultX;
gp.player.solidArea.y = gp.player.solidAreaDefaultY;
eventRect[col][row].x = eventRect[col][row].eventRectDefaultX;
eventRect[col][row].y = eventRect[col][row].eventRectDefaultY;
return hit;
}
/*public void teleport(int col, int row, int gameState) {
gp.gameState = gp.gameState;
gp.ui.currentDialogue = "NYOOM";
gp.player.worldX = gp.tileSize * 17;
gp.player.worldY = gp.tileSize * 20;
canTouchEvent = false;
}
*/
public void damagePit(int col, int row, int gameState) {
gp.gameState = gameState;
gp.ui.currentDialogue = "MWAHA! You dun messed up";
gp.player.life -= 1;
//eventRect[col][row].eventDone = true;
canTouchEvent = true;
}
public void healingPool(int col, int row, int gameState) {
if (gp.keyH.enterPressed == true) {
gp.gameState = gameState;
gp.ui.currentDialogue = "Ahh, moistness";
gp.player.life = gp.player.maxLife;
}
}
}
这是我第一次posting,所以如果有我不遵守的礼节,请告诉我:)
你的checkEvent()
方法是错误的:
public void checkEvent() {
//check if character is one tile away from last event
int xDistance = Math.abs(gp.player.worldX = previousEventX);
int yDistance = Math.abs(gp.player.worldY = previousEventY);
int distance = Math.max(xDistance, yDistance);
if (distance > gp.tileSize) {
canTouchEvent = true;
}
if(canTouchEvent == true) {
if (hit(27, 16, "any") == true) {damagePit(27, 16, gp.dialogueState);}
if (hit(27, 18, "any") == true) {healingPool(27, 18, gp.dialogueState);}
}
}
在行 int xDistance = Math.abs(gp.player.worldX = previousEventX);
中,您将 previousEventX
的当前值(即 0)分配给 gp.player.worldX
(将玩家传送到 X 坐标 0)并设置xDistance
到 0(因为你计算 Math.abs(0)
)。
yDistance
的下一行类似。
将这两行更改为
int xDistance = Math.abs(gp.player.worldX - previousEventX);
int yDistance = Math.abs(gp.player.worldY - previousEventY);