无法使用更新方法更改ParticleEffect的位置

时间:2018-03-29 22:43:39

标签: libgdx

我正在构建一个飞扬的鸟式侧卷轴游戏,目前正在实施主要精灵的收藏品,以便在它飞行时收集。我没有移动主精灵,而是使用ParallaxEffect移动背景,并打算将收藏品(称为orbs)移向主精灵(鸟)。 Orbs以随机位置呈现,但即使调用update方法后位置也不会更改。

这是我的CollectibleOrbs.java

public class CollectibleOrbs {
    private static final int ORB_COUNT = 10;
    private Array<Orb> orbs;
    private Orb orb;

    public CollectibleOrbs(){
        orbs = new Array<Orb>();

        for(int i=0;i<ORB_COUNT; i++) {
            orb = new Orb();
            orbs.add(orb);
        }
     }

    public void update(float delta){
        for(Orb orb: orbs){
            orb.update(delta);
        }
    }

    public void render(SpriteBatch sb){
        for(Orb orb:orbs){
            orb.draw(sb);
        }
    }

    private class Orb{
        private ParticleEffect effect;
        private Vector2 position;
        private Random rand;


        public Orb(){
             effect = new ParticleEffect();
             rand = new Random();
             position = new Vector2(rand.nextInt(Gdx.graphics.getWidth()),rand.nextInt(Gdx.graphics.getHeight()));
             effect.load(Gdx.files.internal("particle/orbred.p"),
                               Gdx.files.internal("particle"));
             effect.setPosition(position.x,position.y);
         }

        public void draw(SpriteBatch sb){
             effect.draw(sb,Gdx.graphics.getDeltaTime());
        }

        public void update(float dt){
            if(position.x< 10){
                position.x = rand.nextInt(Gdx.graphics.getWidth());
                position.y = rand.nextInt(Gdx.graphics.getHeight());
            }
            else
            {
                position.x-= 100*dt;
            }
        }
     }
 }

球体被渲染但它们没有移动,而鸟类动画和视差背景确实:

Rendered Orbs are stationary

我在更新我的游戏状态时调用CollectibleOrb类的更新方法,并在传递所需参数时分别调用render方法。如何确保球体在游戏画面上移动?

1 个答案:

答案 0 :(得分:1)

问题是positioneffect向量无关。仅更改position不会改变effect's职位。解决问题的一种方法:

public void update(float dt){
    if(position.x< 10){
        position.x = rand.nextInt(Gdx.graphics.getWidth());
        position.y = rand.nextInt(Gdx.graphics.getHeight());
    }
    else
    {
        position.x-= 100*dt;
    }
    // you should update ParticleEffect position too, just like you did in the constructor
    effect.setPosition(position.x, position.y); 
}