libgdx 中的物体速度

Object Speed in libgdx

我正在学习 libgdx 开发,但在编辑演示时遇到问题:

我正在编辑一个官方的 flappy bird 演示,我移除了岩石并添加了一些子弹(从右到左)这是我的代码:

public class FGame implements Screen {

// General constants
private static final float PLANE_JUMP_IMPULSE = 350;
private static final float GRAVITY = -20;
private static final float PLANE_VELOCITY_X = 200;
private static final float PLANE_START_Y = 240;
private static final float PLANE_START_X = 50;
private static final float BULLET_SPEED_X = 5;
// Timer constants
private static final float TIME_BULLET_REPEAT = 2;
private static final float TIME_FUEL_REPEAT = 5;
private static final float TIME_FUEL_DECREASE = 5;

// Render && Batch
ShapeRenderer shapeRenderer;
SpriteBatch batch;

// Camera
OrthographicCamera camera;
OrthographicCamera uiCamera;

// Texture from Background && Ceiling
Texture background;
TextureRegion ground;
float groundOffsetX = 0;
TextureRegion ceiling;

// Plane animation && position && speed && gravity
Animation plane;
Vector2 planePosition = new Vector2();
Vector2 planeVelocity = new Vector2();
float planeStateTime = 0;
Vector2 gravity = new Vector2();

// Ready && Game Over signs
TextureRegion ready;
TextureRegion gameOver;

// Font used for Fuel% && Score
BitmapFont font;

// Bullet texture && Sprite && ArrayList && timer from bullets
Texture bulletTexture;
Sprite bulletSprite;
ArrayList<Bullet> bulletList;
float bulletTime;

// Fuel texture && Sprite && ArrayList && Timer
Texture fuelTexture;
Sprite fuelSprite;
ArrayList<Fuel> fuelList;
float fuelTime;

// GameState && Rectangles
GameState gameState = GameState.Start;
int score = 0;
Rectangle rect1 = new Rectangle();

// Music and effects
Music music;
Sound explode;

private void resetWorld() { // Used in the first time and in GameOver
    score = 0;
    groundOffsetX = 0;
    planePosition.set(PLANE_START_X, PLANE_START_Y);
    planeVelocity.set(0, 0);
    gravity.set(0, GRAVITY);
    camera.position.x = 400;
    if (!bulletList.isEmpty())
        bulletList.removeAll(bulletList); // Remove objects every new Game
    if (!fuelList.isEmpty())
        fuelList.removeAll(fuelList);
}

private void updateWorld() { // Used every frame update
    float deltaTime = Gdx.graphics.getDeltaTime();
    planeStateTime += deltaTime;
    bulletTime += deltaTime;
    fuelTime += deltaTime;

    if (Gdx.input.justTouched()) {
        if (gameState == GameState.Start) {
            gameState = GameState.Running;
        }
        if (gameState == GameState.Running) {
            planeVelocity.set(PLANE_VELOCITY_X, PLANE_JUMP_IMPULSE);
        }
        if (gameState == GameState.GameOver) {
            gameState = GameState.Start;
            resetWorld();
        }
    }

    if (gameState != GameState.Start) {
        planeVelocity.add(gravity);
        // Timer functions
        if (bulletTime >= TIME_BULLET_REPEAT) { // Create bullets over time.
            bulletTime -= TIME_BULLET_REPEAT;
            createBullet();
        }
        if (fuelTime >= TIME_FUEL_REPEAT) { // Create fuel over time
            fuelTime -= TIME_FUEL_REPEAT;
            createFuel();
        }
        if (!bulletList.isEmpty()) { // bullets movement.
            for (Bullet bullet : bulletList) {
                bullet.bulletSprite.setX(bullet.bulletSprite.getX()
                        - BULLET_SPEED_X);
            }
        }
    }

    planePosition.mulAdd(planeVelocity, deltaTime);

    camera.position.x = planePosition.x + 350;
    if (camera.position.x - groundOffsetX > ground.getRegionWidth() + 400) {
        groundOffsetX += ground.getRegionWidth();
    }

    rect1.set(planePosition.x + 20, planePosition.y,
            plane.getKeyFrames()[0].getRegionWidth() - 20,
            plane.getKeyFrames()[0].getRegionHeight());

    if (planePosition.y < ground.getRegionHeight() - 20
            || planePosition.y + plane.getKeyFrames()[0].getRegionHeight() > 480 - ground
                    .getRegionHeight() + 20) {
        if (gameState != GameState.GameOver)
            explode.play();
        gameState = GameState.GameOver;
        planeVelocity.x = 0;
    }
}

private void createBullet() {
    bulletList.add(new Bullet(Gdx.graphics.getWidth(), bulletSprite));

    for (Bullet bullet : bulletList) {
        if (!bullet.isSet()) {
            bullet.bulletSprite.setBounds(planePosition.x + 1000,
                    bullet.getY(), 100, 100);
            bullet.Set();
            score++;
        }
    }
}

private void createFuel() {
    fuelList.add(new Fuel(Gdx.graphics.getWidth(), fuelSprite));

    for (Fuel fuel : fuelList) {
        if (!fuel.isSet()) {
            fuel.fuelSprite.setBounds(planePosition.x + 1000,
                    fuel.getY(), 50, 50);
            fuel.Set();
            score++;
        }
    }
}

private void drawWorld() {
    camera.update();
    batch.setProjectionMatrix(camera.combined);
    batch.begin();
    batch.draw(background, camera.position.x - background.getWidth() / 2, 0);

    batch.draw(ground, groundOffsetX, 0);
    batch.draw(ground, groundOffsetX + ground.getRegionWidth(), 0);
    batch.draw(ceiling, groundOffsetX, 480 - ceiling.getRegionHeight());
    batch.draw(ceiling, groundOffsetX + ceiling.getRegionWidth(),
            480 - ceiling.getRegionHeight());
    batch.draw(plane.getKeyFrame(planeStateTime), planePosition.x,
            planePosition.y);
    if (!bulletList.isEmpty()) { // Draw Bullets
        for (Bullet bullet : bulletList) {
            bullet.bulletSprite.draw(batch);
        }
    } // End Draw Bullets
    if (!bulletList.isEmpty()) { // Draw Fuel
        for (Fuel fuel : fuelList) {
            fuel.fuelSprite.draw(batch);
        }
    } // End Draw Fuel
    batch.end();

    batch.setProjectionMatrix(uiCamera.combined);
    batch.begin();
    if (gameState == GameState.Start) {
        batch.draw(ready,
                Gdx.graphics.getWidth() / 2 - ready.getRegionWidth() / 2,
                Gdx.graphics.getHeight() / 2 - ready.getRegionHeight() / 2);
    }
    if (gameState == GameState.GameOver) {
        batch.draw(
                gameOver,
                Gdx.graphics.getWidth() / 2 - gameOver.getRegionWidth() / 2,
                Gdx.graphics.getHeight() / 2 - gameOver.getRegionHeight()
                        / 2);
    }
    if (gameState == GameState.GameOver || gameState == GameState.Running) {
        font.draw(batch, "" + score, Gdx.graphics.getWidth() / 2,
                Gdx.graphics.getHeight() - 60);
    }
    batch.end();
}

static enum GameState { // Enum for gameStates
    Start, Running, GameOver
}

@Override
public void show() { // Method called instead of create()

    // Loading shapeRenderer && batch
    shapeRenderer = new ShapeRenderer();
    batch = new SpriteBatch();

    // Loading camera
    camera = new OrthographicCamera();
    camera.setToOrtho(false, 800, 480);
    uiCamera = new OrthographicCamera();
    uiCamera.setToOrtho(false, Gdx.graphics.getWidth(),
            Gdx.graphics.getHeight());
    uiCamera.update();

    // Loading Font
    font = new BitmapFont(Gdx.files.internal("arial.fnt"));

    // Loading Background && Ceiling
    background = new Texture("background.png");
    ground = new TextureRegion(new Texture("ground.png"));
    ceiling = new TextureRegion(ground);
    ceiling.flip(true, true);

    // Plane frames && animation
    Texture frame1 = new Texture("plane1.png");
    frame1.setFilter(TextureFilter.Linear, TextureFilter.Linear);
    Texture frame2 = new Texture("plane2.png");
    Texture frame3 = new Texture("plane3.png");
    plane = new Animation(0.05f, new TextureRegion(frame1),
            new TextureRegion(frame2), new TextureRegion(frame3),
            new TextureRegion(frame2));
    plane.setPlayMode(PlayMode.LOOP);

    // Bullet Textures && List
    bulletTexture = new Texture("img/bullet.png");
    bulletSprite = new Sprite(bulletTexture);
    bulletList = new ArrayList<Bullet>();

    // Fuel Textures && List
    fuelTexture = new Texture("img/fuel.png");
    fuelSprite = new Sprite(fuelTexture);
    fuelList = new ArrayList<Fuel>();

    // Loading Ready && gameOver Signs
    ready = new TextureRegion(new Texture("ready.png"));
    gameOver = new TextureRegion(new Texture("gameover.png"));

    // Music load && play
    music = Gdx.audio.newMusic(Gdx.files.internal("music.mp3"));
    music.setLooping(true);
    music.play();

    // Effect load
    explode = Gdx.audio.newSound(Gdx.files.internal("explode.wav"));

    // Call resetWorld for the first time.
    resetWorld();

}

@Override
public void render(float delta) {
    Gdx.gl.glClearColor(1, 0, 0, 1);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

    updateWorld();
    drawWorld();
}

子弹绘制正确,但由于某种原因,每次创建子弹时 x 的速度都会增加。

这是我编辑子弹 x 移动的地方:

if (!bulletList.isEmpty()) { // bullets movement.
            for (Bullet bullet : bulletList) {
                bullet.bulletSprite.setX(bullet.bulletSprite.getX()
                        - BULLET_SPEED_X);
            }

这是我的子弹class

public class Bullet {
private float y;
private float x;
private boolean bulletSet = false;
public Sprite bulletSprite;

public Bullet(float x, Sprite bulletSprite) {
    this.x = x;
    this.bulletSprite = bulletSprite;

    y = MathUtils.random(50, 450);
}

public boolean isSet() {
    return bulletSet;
}

public void Set() {
    bulletSet = true;
}

public float getY () {
    return y;
}

public float getX () {
    return x;
}

你能找到问题吗?

问题出在这里,在您的 Bullet 构造函数中:

this.bulletSprite = bulletSprite;

你所有的子弹实例都引用同一个精灵,所以你一遍又一遍地改变同一个精灵。相反,您应该在将 sprite 分配给新子弹时复制它。改成

this.bulletSprite = new Sprite(bulletSprite);

顺便说一下,您对 if (!bulletList.isEmpty()) 的所有调用似乎都是多余的,可以删除。 (只是为了简化您的代码)