You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

基于libGDX的Java太空侵略者克隆:玩家飞船无法向右移动

问题:LibGDX开发太空侵略者,玩家飞船无法向右移动

我正在开发一个Java项目,制作《太空侵略者》(Space Invaders)的克隆版。在实现玩家飞船移动功能时,我从StackOverflow找到了一段代码:

if(Gdx.input.isKeyPressed(Input.Keys.LEFT) )
    x -= Gdx.graphics.getDeltaTime() * PlayerSpeed;
if(Gdx.input.isKeyPressed(Input.Keys.RIGHT) )
    x += Gdx.graphics.getDeltaTime() * PlayerSpeed;

我将这段代码应用到了PlayerShip类中:

public class PlayerShip extends Ship {
    private Animator animator;
    private float PlayerSpeed = 20.0f;
    private int x,y;

    public PlayerShip(SpriteBatch batch){
        this.animator=new Animator(batch,"ship.png", 5, 2);
    }

    public void create(){
        animator.create();
    }

    public void render(){
        this.animator.render(this.x,this.y);
        if(Gdx.input.isKeyPressed(Input.Keys.LEFT) )
            x -= Gdx.graphics.getDeltaTime() * PlayerSpeed;
        if(Gdx.input.isKeyPressed(Input.Keys.RIGHT) )
            x += Gdx.graphics.getDeltaTime() * PlayerSpeed;
    }

    public void setX(int x) {
        this.x = x;
    }

    public void setY(int y) {
        this.y = y;
    }
}

主游戏类Game代码如下:

public class Game extends ApplicationAdapter {
    private SpriteBatch batch;

    private BackgroundManagement backgroundManagement;
    private BitmapFont font;

    private PlayerShip player;
    private SmallShip smallShip;


    @Override
    public void create() {
        Gdx.graphics.setWindowedMode(600, 800);
        batch = new SpriteBatch();
        player = new PlayerShip(batch);
        smallShip = new SmallShip(batch);
        player.create();
        player.setX(300);
        player.setY(100);
        smallShip.create();
        smallShip.setX(200);
        smallShip.setY(400);
        font = new BitmapFont(Gdx.files.internal("gamefont.fnt"),
                Gdx.files.internal("gamefont.png"), false);
        backgroundManagement = new BackgroundManagement(batch);
    }

    @Override
    public void render() {
        Gdx.gl.glClearColor(0, 0, 0.2f, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
        batch.begin();
        backgroundManagement.render();
        player.render();
        smallShip.render();
        batch.end();
    }

    @Override
    public void dispose() {
        batch.dispose();
    }
}

测试时发现飞船无法向右移动,尝试多种解决方案无效,恳请帮助。


解决方案

核心问题:整数类型截断导致移动失效

你的PlayerShip类中,x和y被定义为int类型,但移动计算时Gdx.graphics.getDeltaTime() * PlayerSpeed是浮点数值:

  • 向右移动时,比如PlayerSpeed=20,deltaTime通常在0.016左右,计算结果是0.32,加到int类型的x上时,小数部分会被直接截断,x的整数值不会变化,所以看起来飞船没动。
  • 向左移动时,x -= 0.32会让整数x减1(比如300变成299),所以能看到移动效果。

修复步骤

  1. 修改坐标类型为float
    把PlayerShip中的x、y类型从int改成float,同时更新对应的setter方法:
public class PlayerShip extends Ship {
    private Animator animator;
    private float PlayerSpeed = 20.0f;
    // 改成float类型
    private float x,y;

    public PlayerShip(SpriteBatch batch){
        this.animator=new Animator(batch,"ship.png", 5, 2);
    }

    public void create(){
        animator.create();
    }

    public void render(){
        this.animator.render(this.x,this.y);
        if(Gdx.input.isKeyPressed(Input.Keys.LEFT) )
            x -= Gdx.graphics.getDeltaTime() * PlayerSpeed;
        if(Gdx.input.isKeyPressed(Input.Keys.RIGHT) )
            x += Gdx.graphics.getDeltaTime() * PlayerSpeed;
    }

    // 更新setter为float参数
    public void setX(float x) {
        this.x = x;
    }

    public void setY(float y) {
        this.y = y;
    }
}
  1. 调整主类中的坐标设置
    在Game类的create方法中,给player.setX()和player.setY()传入浮点数值(或者直接传int,会自动转float):
player.setX(300f);
player.setY(100f);
  1. 可选:提升移动速度
    当前PlayerSpeed=20移动速度偏慢,可以适当调高数值,比如改成200f,让移动效果更明显:
private float PlayerSpeed = 200.0f;

内容的提问来源于stack exchange,提问作者PedroPereira14

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.10 17:50:45