Sanjeev Tripathi
Sanjeev Tripathi

Reputation: 53

Unable to change position of ParticleEffect using update method

I am building a flappy bird style side scroller game and currently implementing collectible items for the main sprite to collect as it flies. I am not moving the main sprite but moving the background using ParallaxEffect and intend to move the collectibles (called orbs) towards the main sprite (bird). The Orbs are rendered in random positions but the position is not changed even after the update method is called.

Here is my 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;
            }
        }
     }
 }

The orbs are rendered but they are not moving whereas the bird animation and the parallax background does:

Rendered Orbs are stationary

I am calling the update method of CollectibleOrb class in the update of my game state and respectively for the render method while passing required parameters. How do to make sure the orbs move on the game screen?

Upvotes: 0

Views: 186

Answers (1)

Arctic45
Arctic45

Reputation: 1098

The problem is that position is just unrelated to effect vector. Changing just position won't change effect's position. One way to solve it:

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); 
}

Upvotes: 1

Related Questions