Java 玩家没有';当试图从一个玩家类中抽签时,不要移动

Java 玩家没有';当试图从一个玩家类中抽签时,不要移动,java,libgdx,desktop,Java,Libgdx,Desktop,因此,我目前正在为玩家重构游戏中的代码,并启动了一个玩家类: import com.badlogic.gdx.Gdx; import com.badlogic.gdx.Input; import com.badlogic.gdx.graphics.Texture; import com.badlogic.gdx.graphics.g2d.SpriteBatch; public class Player { protected String name; protected flo

因此,我目前正在为玩家重构游戏中的代码,并启动了一个玩家类:

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;

public class Player
{
    protected String name;
    protected float health, maxHealth;
    protected Texture texture;
    protected int xPos, yPos;

    public Player(String name, float maxHealth, Texture texture) {
        this(name, maxHealth, texture, 0, 0);
    }

    public Player(String name, float maxHealth, Texture texture, int xPos, int yPos) {
        this.name = name;
        this.health = this.maxHealth = maxHealth;
        this.texture = texture;
        this.xPos = xPos;
        this.yPos = yPos;
    }

    public Texture getTexture() {
        return this.texture;
    }

    public int getX() {
        return xPos;
    }
    public int getY() {
        return yPos;
    }

    public String getName() {
        return name;
    }

    public void draw(SpriteBatch spriteBatch) {
        spriteBatch.begin();
        spriteBatch.draw(texture, xPos, yPos, 0.5f, 0.5f, 0, 0,
                texture.getWidth(), texture.getHeight(), false, false);
        spriteBatch.end();
    }

    public void update(float delta) {
        processMovement(delta);
    }

    public void processMovement(float delta) {
        if(Gdx.input.isKeyPressed(Input.Keys.A)) {
            xPos -= 50 * delta;
        }
        if(Gdx.input.isKeyPressed(Input.Keys.D)) {
            xPos += 50 * delta;
        }
    }
}
我使用的是正交摄影机,我还没有添加任何地形,因为我下一步要添加地形,但是我希望玩家始终保持在中间,但在我绘制地形时让玩家在地形周围移动

我创建相机和绘制播放器的代码如下:

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;

public class GameState implements Screen
{
    private PixelGame parent;

    private OrthographicCamera camera;
    private Player player;
    private Texture playerTexture;
    private SpriteBatch spriteBatch;

    public GameState(PixelGame parent) {
        this.parent = parent;
        this.playerTexture = new Texture(Gdx.files.internal("player/sprite.png"));
        this.player = new Player("Me", 20, playerTexture);
        this.spriteBatch = new SpriteBatch();
    }

    @Override
    public void render(float delta) {
        camera.update();
        spriteBatch.setProjectionMatrix(camera.combined);

        Gdx.gl.glClearColor(0, 0, 0.2f, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

        player.draw(spriteBatch);
        player.update(delta);
    }

    @Override
    public void show() {
    }

    @Override
    public void dispose() {
    }

    @Override
    public void resize(int width, int height) {
        float ratio = (float)width / (float)height;
        camera = new OrthographicCamera(2f * ratio, 2f);
    }

    @Override
    public void pause() {
    }

    @Override
    public void resume() {
    }

    @Override
    public void hide() {
    }
}

玩家精灵似乎没有移动,但当我将值更改为>75时,玩家精灵会在屏幕上疾驰而过。你的类中的xPos和YPO都是INT。我想这就是问题所在。您的
processMovement()
方法每秒调用100次,并且
50*delta
很可能小于1,因此它被四舍五入为0,因为值必须存储在int变量中。尝试将
xPos
yPos
更改为浮动


如果这没有帮助(在不尝试代码的情况下无法确定),请进行一些调试。放断点。查看是否调用了
processMovement()
,如果调用了,那么变量delta的值是多少。计算是如何进行的。

xpo
ypo
在你们班上是整数。我想这就是问题所在。您的
processMovement()
方法每秒调用100次,并且
50*delta
很可能小于1,因此它被四舍五入为0,因为值必须存储在int变量中。尝试将
xPos
yPos
更改为浮动

如果这没有帮助(在不尝试代码的情况下无法确定),请进行一些调试。放断点。查看是否调用了
processMovement()
,如果调用了,那么变量delta的值是多少。如何计算