編輯
我正在嘗試用 Java 創建一個 2D 平臺游戲。我有一個具有 update() 方法的 Player-class:
public class Player {
...
...
...
public void update(TileBlock[][] tb, ArrayList<MovingBlock> movingBlocks) {
// checkBlockCollision(TileBlock[][] tb)
// checkMovingBlockCollision(ArrayList<MovingBlock> movingBlocks)
// Bounds for collision
int x1 = (int) (x width GameState.xOffset);
int y1 = (int) (y height GameState.yOffset);
int x2 = (int) (x GameState.xOffset);
int y2 = (int) (y GameState.yOffset);
// Tile Block Collision
for (TileBlock[] tileBlocks : tb) {
for (int x = 0; x < tb[0].length; x ) {
if (tileBlocks[x].getID() == 1) {
// Right collision
if (Collision.playerBlocked(new Point(x1, y2 - 2), tileBlocks[x]) ||
Collision.playerBlocked(new Point(x1, y1 - 1), tileBlocks[x])) {
right = false;
}
// Left collision
if (Collision.playerBlocked(new Point(x2 - 1, y2 2), tileBlocks[x]) ||
Collision.playerBlocked(new Point(x2 - 1, y1 - 1), tileBlocks[x])) {
left = false;
}
// Top Collision
if (Collision.playerBlocked(new Point(x2 1, y2), tileBlocks[x]) ||
Collision.playerBlocked(new Point(x1 - 2, y2), tileBlocks[x])) {
jumping = false;
falling = true;
}
// Bottom collision
if (Collision.playerBlocked(new Point(x2 2, y1 1), tileBlocks[x]) ||
Collision.playerBlocked(new Point(x1 - 2, y1 1), tileBlocks[x])) {
y = tileBlocks[x].getY() - height - GameState.yOffset;
falling = false;
topCollision = true;
}
if (!topCollision && !jumping) {
falling = true;
}
} else if (tileBlocks[x].getID() == 2) {
// Right collision
if (Collision.playerBlocked(new Point(x1, y2 - 2), tileBlocks[x]) ||
Collision.playerBlocked(new Point(x1, y1 - 1), tileBlocks[x])) {
reachedGoal = true;
}
// Left collision
if (Collision.playerBlocked(new Point(x2 - 1, y2 2), tileBlocks[x]) ||
Collision.playerBlocked(new Point(x2 - 1, y1 - 1), tileBlocks[x])) {
reachedGoal = true;
}
// Top Collision
if (Collision.playerBlocked(new Point(x2 1, y2), tileBlocks[x]) ||
Collision.playerBlocked(new Point(x1 - 2, y2), tileBlocks[x])) {
reachedGoal = true;
}
// Bottom collision
if (Collision.playerBlocked(new Point(x2 2, y1 1), tileBlocks[x]) ||
Collision.playerBlocked(new Point(x1 - 2, y1 1), tileBlocks[x])) {
reachedGoal = true;
}
}
}
}
// Moving Block Collision
if (movingBlocks != null && !movingBlocks.isEmpty()){
for (MovingBlock movingBlock : movingBlocks) {
if (movingBlock.getID() != 0) {
// Right collision
if (Collision.playerMovingBlock(new Point(x1, y2 - 2), movingBlock) ||
Collision.playerMovingBlock(new Point(x1, y1 - 1), movingBlock)) {
right = false;
}
// Left collision
if (Collision.playerMovingBlock(new Point(x2 - 1, y2 2), movingBlock) ||
Collision.playerMovingBlock(new Point(x2 - 1, y1 - 1), movingBlock)) {
left = false;
}
// Top Collision
if (Collision.playerMovingBlock(new Point(x2 1, y2), movingBlock) ||
Collision.playerMovingBlock(new Point(x1 - 2, y2), movingBlock)) {
jumping = false;
falling = true;
}
// Bottom collision
if (Collision.playerMovingBlock(new Point(x2 2, y1 1), movingBlock) ||
Collision.playerMovingBlock(new Point(x1 - 2, y1 1), movingBlock)) {
y = movingBlock.getY() - height - GameState.yOffset;
falling = false;
topCollision = true;
// Move the player the same amount the block is moving
GameState.xOffset = movingBlock.getMove();
} else {
if (!topCollision && !jumping) {
falling = true;
}
}
}
}
}
topCollision = false;
double moveSpeed = 2.5;
if (right) {
GameState.xOffset = moveSpeed; // Move the tile-block to the left because the player is moving right
}
if (left){
GameState.xOffset -= moveSpeed; // Move the tile-block to the right because the player is moving left
}
if (jumping){
GameState.yOffset -= currentJumpSpeed;
currentJumpSpeed -= 0.1; // Jump is slowing down at 0.1 pixel per call to update()
if (currentJumpSpeed <= 0){
currentJumpSpeed = startSpeed;
jumping = false;
falling = true;
}
}
if (falling){
GameState.yOffset = currentFallSpeed;
// Fall-speed
double maxFallSpeed = 5;
if (currentFallSpeed < maxFallSpeed){
currentFallSpeed = 0.1;
}
}
if (!falling) { currentFallSpeed = 0.1; } // So we fall again without going too fast in the start
// Player/Enemy collision
if (Collision.PECollision() && !invincible) {
health--;
invincible = true;
startTime = System.currentTimeMillis();
}
if (invincible) {
int deathDuration = 2000; //in milliseconds
long duration = System.currentTimeMillis() - startTime;
if (duration > deathDuration){
invincible = false;
}
}
}
}
此代碼有效,但update()非常大。所以我試圖提取這兩種碰撞方法,所以我只在update(). 但是當我嘗試將上面的代碼更改為:
public void update(TileBlock[][] tb, ArrayList<MovingBlock> movingBlocks) {
checkBlockCollision(tb);
checkMovingBlockCollision(movingBlocks);
topCollision = false;
double moveSpeed = 2.5;
if (right) {
GameState.xOffset = moveSpeed; // Move the tile-block to the left because the player is moving right
}
if (left){
GameState.xOffset -= moveSpeed; // Move the tile-block to the right because the player is moving left
}
if (jumping){
GameState.yOffset -= currentJumpSpeed;
currentJumpSpeed -= 0.1; // Jump is slowing down at 0.1 pixel per call to update()
if (currentJumpSpeed <= 0){
currentJumpSpeed = startSpeed;
jumping = false;
falling = true;
}
}
if (falling){
GameState.yOffset = currentFallSpeed;
// Fall-speed
double maxFallSpeed = 5;
if (currentFallSpeed < maxFallSpeed){
currentFallSpeed = 0.1;
}
}
if (!falling) { currentFallSpeed = 0.1; } // So we fall again without going too fast in the start
// Player/Enemy collision
if (Collision.PECollision() && !invincible) {
health--;
invincible = true;
startTime = System.currentTimeMillis();
}
if (invincible) {
int deathDuration = 2000; //in milliseconds
long duration = System.currentTimeMillis() - startTime;
if (duration > deathDuration){
invincible = false;
}
}
}
private void checkMovingBlockCollision(ArrayList<MovingBlock> movingBlocks) {
// Moving Block Collision
if (movingBlocks != null && !movingBlocks.isEmpty()){
for (MovingBlock movingBlock : movingBlocks) {
if (movingBlock.getID() != 0) {
// Right collision
if (Collision.playerMovingBlock(new Point(x1, y2 - 2), movingBlock) ||
Collision.playerMovingBlock(new Point(x1, y1 - 1), movingBlock)) {
right = false;
}
// Left collision
if (Collision.playerMovingBlock(new Point(x2 - 1, y2 2), movingBlock) ||
Collision.playerMovingBlock(new Point(x2 - 1, y1 - 1), movingBlock)) {
left = false;
}
// Top Collision
if (Collision.playerMovingBlock(new Point(x2 1, y2), movingBlock) ||
Collision.playerMovingBlock(new Point(x1 - 2, y2), movingBlock)) {
jumping = false;
falling = true;
}
// Bottom collision
if (Collision.playerMovingBlock(new Point(x2 2, y1 1), movingBlock) ||
Collision.playerMovingBlock(new Point(x1 - 2, y1 1), movingBlock)) {
y = movingBlock.getY() - height - GameState.yOffset;
falling = false;
topCollision = true;
// Move the player the same amount the block is moving
GameState.xOffset = movingBlock.getMove();
} else {
if (!topCollision && !jumping) {
falling = true;
}
}
}
}
}
}
private void checkBlockCollision(TileBlock[][] tb) {
// Tile Block Collision
for (TileBlock[] tileBlocks : tb) {
for (int x = 0; x < tb[0].length; x ) {
if (tileBlocks[x].getID() == 1) {
// Right collision
if (Collision.playerBlocked(new Point(x1, y2 - 2), tileBlocks[x]) ||
Collision.playerBlocked(new Point(x1, y1 - 1), tileBlocks[x])) {
right = false;
}
// Left collision
if (Collision.playerBlocked(new Point(x2 - 1, y2 2), tileBlocks[x]) ||
Collision.playerBlocked(new Point(x2 - 1, y1 - 1), tileBlocks[x])) {
left = false;
}
// Top Collision
if (Collision.playerBlocked(new Point(x2 1, y2), tileBlocks[x]) ||
Collision.playerBlocked(new Point(x1 - 2, y2), tileBlocks[x])) {
jumping = false;
falling = true;
}
// Bottom collision
if (Collision.playerBlocked(new Point(x2 2, y1 1), tileBlocks[x]) ||
Collision.playerBlocked(new Point(x1 - 2, y1 1), tileBlocks[x])) {
y = tileBlocks[x].getY() - height - GameState.yOffset;
falling = false;
topCollision = true;
}
if (!topCollision && !jumping) {
falling = true;
}
} else if (tileBlocks[x].getID() == 2) {
// Right collision
if (Collision.playerBlocked(new Point(x1, y2 - 2), tileBlocks[x]) ||
Collision.playerBlocked(new Point(x1, y1 - 1), tileBlocks[x])) {
reachedGoal = true;
}
// Left collision
if (Collision.playerBlocked(new Point(x2 - 1, y2 2), tileBlocks[x]) ||
Collision.playerBlocked(new Point(x2 - 1, y1 - 1), tileBlocks[x])) {
reachedGoal = true;
}
// Top Collision
if (Collision.playerBlocked(new Point(x2 1, y2), tileBlocks[x]) ||
Collision.playerBlocked(new Point(x1 - 2, y2), tileBlocks[x])) {
reachedGoal = true;
}
// Bottom collision
if (Collision.playerBlocked(new Point(x2 2, y1 1), tileBlocks[x]) ||
Collision.playerBlocked(new Point(x1 - 2, y1 1), tileBlocks[x])) {
reachedGoal = true;
}
}
}
}
}
碰撞沒有被記錄,玩家只是從地圖上掉下來,超出了界限,而不是像預期的那樣落在地圖上的塊上。在這里提取方法的正確方法是什么?感謝所有幫助!
uj5u.com熱心網友回復:
我發現了一個可能導致您的問題的錯誤。
TLDR;解決方案
在已經重構的代碼中,您似乎只初始化了邊界值,這部分:
// Bounds for collision
int x1 = (int) (x width GameState.xOffset);
int y1 = (int) (y height GameState.yOffset);
int x2 = (int) (x GameState.xOffset);
int y2 = (int) (y GameState.yOffset);
在原始代碼中,您似乎每次進入該update()方法時都會對其進行初始化。因此,我相信您只需將這些初始化移動到它們所屬的位置即可使其作業。
在最新的 Intellij 中提取方法的正確方法
這是從 Intellij 的任何代碼中提取方法的簡單方法:
- 選擇要提取的代碼
- 右鍵,然后展開
Refactor - 在下
Refactor,你應該看到Extract Method...,左鍵單擊它 - 然后,您只需要按照向導設定方法名稱,每個引數等
編輯
你在我回復的時候添加了評論,不知道你已經嘗試過自動方法提取;但是我的訊息的第一部分仍然有效以修復錯誤
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/464775.html
