0

想出这个问题的标题真的很困难。所以我正在制作一个游戏,并且我有一个用于处理移动的方法,称为handleVelocity(). 它的工作方式是,在我的游戏中,它会更新每个滴答声,寻找角色移动。如果角色移动低于 1 或 -1,则它完全停止角色移动。所以我将角色的移动设置为0。但是,角色不能再移动,我不知道如何让他继续移动。我如何处理这个速度以便它可以继续移动,即使它之前已经停止了。我将在下面发布这两个课程。

球员等级:

import java.io.IOException;

import org.newdawn.slick.opengl.Texture;
import org.newdawn.slick.opengl.TextureLoader;
import org.newdawn.slick.util.ResourceLoader;

public class Player {

    public Texture playerTexture;

    // Positions & speed
    public float xPos = 20.0f; // This is initial
    public float yPos = 0.0f; // Same as above.
    public float acceleration = 15;

    public static int gravityForce = 6;
    public static int jumpVelocity = 100;
    private float moveSpeed = 4f;
    private static int MAX_MOVE_SPEED = 9;

    public boolean isSupported = true; // Once again, initial value.
    public boolean goingRight, goingLeft, canJump;

    // movement methods & constants

    public void update() {
        handleVelocity();
        applyGravity();
        checkForSupport();
    }

    public void handleVelocity() {
        float minMoveSpeed = 1;
        if (this.moveSpeed < minMoveSpeed && this.moveSpeed > -minMoveSpeed) {
            this.moveSpeed = 0;
        } else {
            float dampening = 0.00002f;
            double sign = -(int) Math.signum(moveSpeed);
            this.moveSpeed += (float) (dampening * sign);
        }
        xPos += this.moveSpeed;
    }

    public Texture grabTexture() {
        try {
            playerTexture = TextureLoader.getTexture("PNG",
                    ResourceLoader.getResourceAsStream("res/test_char.png"));
        } catch (IOException e) {
            e.printStackTrace();
        }
        return playerTexture;
    }

    private void checkForSupport() {
        if (yPos == 0) {
            isSupported = true;
        } else if (yPos > 0 /* and is not on a platform */) {
            isSupported = false;
        }
    }

    private void applyGravity() {
        if (!isSupported) {
            yPos -= gravityForce;
        }
    }

    public void printPos(String moveMethod) {
        System.out.println(moveMethod + " X: " + xPos + " Y: " + yPos
                + " Going Right: " + goingRight + " Going Left: " + goingLeft
                + " Speed: " + moveSpeed);
    }

    // movement methods

    private void accelerateX(float speed) {
        moveSpeed += (float) (speed * 0.0096);
        if (moveSpeed >= MAX_MOVE_SPEED) {
            moveSpeed = MAX_MOVE_SPEED;
        } else if (moveSpeed <= -MAX_MOVE_SPEED) {
            moveSpeed = -MAX_MOVE_SPEED;
        }
    }

    @SuppressWarnings("unused")
    private void accelerateY() {

    }

    public void moveRight() {
        printPos("Moving Right!");
        accelerateX(acceleration);
    }

    public void moveLeft() {
        printPos("Moving Left!");
        accelerateX(-acceleration);
    }

    public void jump() {
        printPos("Jumping!");
    }

}

主类:

import com.hasherr.platformer.entity.Player;

import org.lwjgl.LWJGLException;
import org.lwjgl.input.Keyboard;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import static org.lwjgl.opengl.GL11.*;

public class Main {

    private void display() {
        try {
            Display.setDisplayMode(new DisplayMode(1000, 550));
            Display.setTitle("Unnamed Platformer Game");
            Display.create();
        } catch (LWJGLException e) {
            e.printStackTrace();
            System.exit(0);
        }

        // OpenGL

        while (!Display.isCloseRequested()) {
            Display.update();
            Display.sync(60); // sync to 60 fps
            initGL();

            player.update();
            handleKeyboardInput();
        }

        Display.destroy();
    }

    private boolean keyboardInUse() {
        boolean keyboardInUse;
        if (!(Keyboard.isKeyDown(Keyboard.KEY_A)
                || Keyboard.isKeyDown(Keyboard.KEY_D) || Keyboard
                    .isKeyDown(Keyboard.KEY_SPACE))) {
            keyboardInUse = false;
        } else {
            keyboardInUse = true;
        }

        return keyboardInUse;
    }

    private void handleKeyboardInput() {
        if (!keyboardInUse()) {
            player.goingLeft = false;
            player.goingRight = false;
        }

        if (Keyboard.isKeyDown(Keyboard.KEY_D)) {   
            player.goingLeft = false;
            player.goingRight = true;
            player.moveRight();
        } else if (Keyboard.isKeyDown(Keyboard.KEY_A)) {
            player.goingLeft = true;
            player.goingRight = false;
            player.moveLeft();
        } else if (Keyboard.isKeyDown(Keyboard.KEY_SPACE)) {
            player.jump();
        }

    }

    private void initGL() {
        // initial OpenGL items for 2D rendering
        glClear(GL_COLOR_BUFFER_BIT);
        glEnable(GL_TEXTURE_2D);
        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();
        glOrtho(0, 1000, 0, 550, 1, -1);

        // start rendering player image
        player.grabTexture().bind();
        glBegin(GL_QUADS);

        glTexCoord2f(0, 0);
        glVertex2f(player.xPos, player.yPos);

        glTexCoord2f(1, 0);
        glVertex2f(player.xPos + 150, player.yPos);

        glTexCoord2f(1, 1);
        glVertex2f(player.xPos + 150, player.yPos + 150);

        glTexCoord2f(0, 1);
        glVertex2f(player.xPos, player.yPos + 150);
        glEnd(); // stop rendering this image
    }

    Player player = new Player();

    public static void main(String[] args) {
        Main main = new Main();
        main.display();
    }
}
4

2 回答 2

2

是什么触发了运动?假设当按下右箭头键并且当前玩家处于站立状态时玩家应该移动,在这种情况下将 moveSpeed 设置回默认值 4f。

于 2013-07-21T03:44:04.350 回答
1

我知道您已经接受了答案,并且提供的答案确实可以完成工作,但是我认为这需要更好的解决方案和更完整的解释。

代码不能按原样工作,因为在您调用的moveLeft和方法中。值为15。让我们看看方法。moveRightaccelerateX(acceleration)accelerationaccelerateX

private void accelerateX(float speed) {
    moveSpeed += (float) (speed * 0.0096);
    if (moveSpeed >= MAX_MOVE_SPEED) {
        moveSpeed = MAX_MOVE_SPEED;
    } else if (moveSpeed <= -MAX_MOVE_SPEED) {
        moveSpeed = -MAX_MOVE_SPEED;
    }
}

它将提供的速度乘以 0.0096。15*.0096 = .144。所以在这里我们将 .144 添加到moveSpeed. 现在让我们去handleVelocity

public void handleVelocity() {
    float minMoveSpeed = 1;
    if (this.moveSpeed < minMoveSpeed && this.moveSpeed > -minMoveSpeed) {
        this.moveSpeed = 0;
    } else {
        float dampening = 0.00002f;
        double sign = -(int) Math.signum(moveSpeed);
        this.moveSpeed += (float) (dampening * sign);
    }
    xPos += this.moveSpeed;
}

如果小于 1,则此代码设置moveSpeed为零,大于 -1 以强制玩家停止移动。

这可以通过多种方式修复并保持我们漂亮的伪物理。您接受的答案通过将移动速度强制为某个值来破坏物理系统。也许这就是你在平台游戏中想要的,也许不是。你的选择。

我建议您减少minMoveSpeed到较小的值,例如 0.01。

于 2013-07-21T04:41:09.513 回答