首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >尝试从Player类绘制时,Player不会移动

尝试从Player类绘制时,Player不会移动
EN

Stack Overflow用户
提问于 2017-11-22 04:08:51
回答 1查看 66关注 0票数 0

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

代码语言:javascript
复制
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;
        }
    }
}

我使用的是正交相机,我还没有添加任何地形,因为我下一步要做的是,但是我希望玩家始终留在中心,但当我绘制地形时,让玩家在地形周围移动。

我用来创建摄像头和绘制播放器的代码如下:

代码语言:javascript
复制
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() {
    }
}

player sprite似乎不会移动,但当我将值更改为>75时,player sprite会像无人事务一样在屏幕上冲刺。

EN

回答 1

Stack Overflow用户

发布于 2017-11-22 17:54:20

你的类中的xPosyPos是整型的。我会说这就是问题所在。您的processMovement()方法每秒被调用100次,而50 * delta很可能小于1,因此取整为0,因为必须将值存储在整型变量中。尝试将xPosyPos更改为浮点数。

如果这不起作用(如果不尝试代码就不能确定),那么就进行一些调试。设置断点。查看是否调用了processMovement(),如果是,那么变量delta的值是什么。如何计算。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47421803

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档