java - 一边跳跃一边移动

标签 java while-loop 2d

我需要能够在他跳跃时移动我的角色。问题是我不希望角色以传统方式移动。他是方形的,他在地上时应该这样移动:

enter image description here

我不希望他在动画完成之前停止移动。但是当他同时跳跃和移动时,就没有那样的动画了。当他在空中时,这个 Action 变得经典。

我做了一些测试代码,我设法得到了我想要的角色在地面上的移动,但没有得到我想要的在空中的正常横向移动。

我应该先给你看我的模型(Booby 是角色的名字):

public class Booby {

    int posX;
    int posY;
    boolean movingRight;
    boolean movingLeft;

    Booby() {
        posX = 0;
        posY = 500;
    }

    int getPosX() {
        return posX;
    }

    int getPosY() {
        return posY;
    }

    void move(int x, int y) {
        posX += x;
        posY += y;
    }
}

这是我的 Controller :

public class Controller extends KeyAdapter implements ActionListener {
    Timer loop;

    Booby booby;

    boolean right;
    boolean left;
    boolean up;

    int countUp = 0;
    int jump = 0;
    int countLeft = 0;
    int countRight = 0;


    Controller(Booby b, View v) {
        booby = b;
        loop = new Timer(0, this);
    }

    // Key events
    public void keyPressed(KeyEvent e) {
        int code = e.getKeyCode();
        switch (code) {
        case KeyEvent.VK_UP:
            up = true;
            right = false;
            left = false;
            loop.start();
            break;

        case KeyEvent.VK_RIGHT:
            right = true;
            left = false;
            up = false;
            loop.start();
            break;

        case KeyEvent.VK_LEFT:
            left = true;
            up = false;
            right = false;
            loop.start();
            break;

        }
    }

    public void actionPerformed(ActionEvent evt) {
        if (up) {
            countUp++;
            jump++;
                    // When UP is pressed, it moves up a bit 10 times...
            if (countUp <= 100 && countUp > 0) {
                booby.move(0, -1);
            }
                    // ...Then it moves down a bit 10 times
            else if (countUp > 100) {
                if (jump <= 200) {
                    booby.move(0, 1);
                } else if (jump > 200) {
                    loop.stop();
                    jump = 0;
                    countUp = 0;
                }
            }
        }

            // When Right is pressed, it moves a bit 10 times to the right
        else if (right) {
            booby.movingRight = true;
            countRight++;
            if (countRight <= 315) {
                booby.move(1, 0);
            } else {
                countRight = 0;
                loop.stop();
                booby.movingRight = false;
            }
        }
            // When Leftis pressed, it moves a bit 10 times to the left
        else if (left) {
            booby.movingLeft = true;
            countLeft++;
            if (countLeft <= 315) {
                booby.move(-1, 0);
            } else {
                countLeft = 0;
                loop.stop();
                booby.movingLeft = false;
            }
        }
    }
}

我还有一个包含动画的 JPanel :

if (booby.movingRight) {
    imgCharacter = new ImageIcon("images/booby_move_right.gif");
} else if (booby.movingLeft) {
    imgCharacter = new ImageIcon("images/booby_move_left.gif");
} else {
    imgCharacter = new ImageIcon("images/booby.png");
}
Image personnage = imgCharacter.getImage();

g.drawImage(personnage, booby.getPosX() * 1, booby.getPosY() * 1, null);

repaint();

现在,他可以左右移动,甚至可以跳跃。但如果你在他跳跃时按右,它会停止跳跃并向右移动。

我想要的是当他跳跃时,例如你按下右,它只向右移动一次。因此,如果您一直按向右并在跳跃,它会慢慢向右移动。

最佳答案

我会采用这种方法:左右键应该对角色的垂直移动没有影响,而跳跃键应该对水平移动没有影响。 我认为您可以通过更改将键“映射”到 Action 的方式来改变这种行为。像这样:

switch (code) {
    case KeyEvent.VK_UP:
        up = true;
        //right = false;
        //left = false;
        loop.start();
        break;

    case KeyEvent.VK_RIGHT:
        right = true;
        left = false;
        //up = false;
        loop.start();
        break;

    case KeyEvent.VK_LEFT:
        left = true;
        //up = false;
        right = false;
        loop.start();
        break;

}

这样,当您按下右/左按钮时,它会向右/向左移动,但不会停止上升!

关于java - 一边跳跃一边移动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16350993/

相关文章:

java - 访问 Youtube API

java - Maven Cobertura 和包没有运行单元测试两次

java - 检查 ArrayList 是否包含来自另一个 ArrayList 的所有元素

javascript - jquery while 循环。查找所有类

while-loop - 使用 "define-syntax-rule"制作我自己的 while 循环

c++ - 如何在 DirectX 中绘制 2D 几何图形和 3D 对象? (D3D9)

java - libGDX 坐标系

java - 我是否需要安装 Apache Spark 和/或 Scala 才能运行 jUnit?

php - fatal error mysql和登录表单

c# - 用鼠标拖动对象并将其捕捉到值