当前位置: 首页 > 知识库问答 >
问题:

如何正确地提取方法在JavaintelliJ?

尉迟明辉
2023-03-14

编辑

我试图在Java中创建一个2D平台游戏。我有一个玩家类,它有一个更新()方法:

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

碰撞没有登记,玩家只是穿过地图并出界,而不是像预期的那样降落在地图上的方块上。这里提取方法的正确方法是什么?感谢所有帮助!

共有1个答案

梁研
2023-03-14

我发现了一个可能导致你问题的错误。

在已经重构的代码中,您似乎只初始化了边界值,这部分:

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

在原始代码中,您似乎每次使用date()方法都会初始化它们。所以我相信您只需要将这些初始化移动到它们属于的地方就可以使它工作。

以下是从Intellij的任何代码中提取方法的简单方法:

  • 选择要提取的代码
  • 右键单击,然后展开重构
  • 重构下,您应该看到提取方法 ,左键单击它
  • 然后,只需按照向导设置方法名、每个参数等

你在我回复的时候添加了注释,所以不知道你已经尝试了自动方法提取;但是我消息的第一部分仍然有效,可以修复错误

 类似资料:
  • 我发现其他人也有同样的问题,他们的问题通过在InputStreamReader构造函数中指定UTF-8来解决: 以UTF-8形式读取InputStream 这对我不起作用,我也不知道为什么。无论我尝试什么,我总是得到转义的unicode值(斜杠-U+十六进制),而不是实际的语言字符。我在这里做错了什么?提前道谢! 请注意:这不是字体问题。我之所以知道这一点,是因为如果我对同一个文件使用Resour

  • 我正在准备Java认证,显然我不能正确回答这个答案。 给定: 它在第11行独立插入,为Chilis实现equals()和hashCode()契约?(选择所有适用内容。) A。public int hashCode(){return 7;} B。public int hashCode(){return hotness;} C。public int hashCode(){return color.le

  • 问题内容: 我正在做一个需要我向API发出请求的项目。用Async / Await发出POST请求的正确形式是什么? 作为示例,这是我获取所有设备列表的信息。我将如何将此请求更改为POST以创建新设备?我知道我必须添加带有数据正文的标头。 问题答案: 实际上,您的代码可以像这样进行改进: 要发布信息,只需在获取调用的设置上添加方法。

  • 对于类中的方法,文档是这样说的: 取消此对话框,将其从屏幕上移除。可以从任何线程安全地调用此方法。请注意,当对话框被取消时,不应重写此方法来进行清理,而应在中实现此方法。 在我的代码中,我所做的就是调用来删除它。但是我没有做其他任何事情,甚至没有使用。因此,我问的是如何正确地删除以避免任何内存泄漏等。

  • 问题内容: 我需要在我的没有超类的对象中实现一个深层克隆。 处理超类(即Object)引发的检查的最佳方法是什么? 一位同事建议我按以下方式处理: 对于我来说,这似乎是一个不错的解决方案,但我想将其扔给StackOverflow社区,以查看是否有我可以提供的其他见解。谢谢! 问题答案: 您绝对必须使用吗?大多数人都同意是坏的。 Josh Bloch谈设计-复制构造函数与克隆 如果您已经阅读了我书中

  • 假设我有一个JAVA客户端应用程序,它试图通过https连接到服务器(example.com)。客户端应用程序有一个信任存储JKS,它有服务器的证书和其他一些证书。在服务器将其证书发送到此客户端应用程序的握手过程中,如何从信任存储jks中提取正确的证书。i、 基于什么参数,java将服务器发送的证书与存储在JKS中的证书相匹配。