1
\$\begingroup\$

I'm making a game in LibGDX where the player dodges falling objects. At the moment, my code makes each object (in this case 'DROPS') fall at random speeds. I'm wondering if anyone can suggest how to amend this code to make all the objects start falling at the same speed and have this value increase over time. Here is my current code:

for (int i = 0; i < amount; i++) {
        float x = MathUtils.random(0, ManicMeltdowns.WIDTH - TextureManager.DROPS.getWidth());
        float y = MathUtils.random(ManicMeltdowns.HEIGHT, ManicMeltdowns.HEIGHT * 3);
        float speed = MathUtils.random(2, 5);
        addEntity(new Drops(new Vector2 (x, y), new Vector2(0, -speed)));
}

I assume you have to adjust 'float speed', but I'm not sure how.

Many thanks.

-Edit- Here is the drops class:

public class Drops extends Entity{

public Drops(Vector2 pos, Vector2 direction) {
    super(TextureManager.DROPS, pos, direction);

}

@Override
public void update() {
    pos.add(direction);

    if (pos.y <= -TextureManager.DROPS.getHeight()) {
        float x = MathUtils.random(0, ManicMeltdowns.WIDTH - TextureManager.DROPS.getWidth());
        pos.set(x, ManicMeltdowns.HEIGHT);
    }

}

}

And here is the entity class:

public abstract class Entity {

protected Texture texture;
protected Vector2 pos, direction;

public Entity(Texture texture, Vector2 pos, Vector2 direction) {
    this.texture = texture;
    this.pos = pos;
    this.direction = direction;
}

public abstract void update();

public void render(SpriteBatch sb) {
    sb.draw(texture,  pos.x,  pos.y);
}

public Vector2 getPosition() {
    return pos;
}

public Rectangle getBounds() {
    return new Rectangle(pos.x, pos.y, texture.getWidth(), texture.getHeight());
}

public void setDirection(float x, float y) {
    direction.set(x, y);
    direction.scl(Gdx.graphics.getDeltaTime());
}

}
\$\endgroup\$
1
  • \$\begingroup\$ it'll require a change to Drops that increases it's speed vector each tick \$\endgroup\$ Feb 12, 2015 at 12:03

1 Answer 1

1
\$\begingroup\$

in drops.update:

@Override
public void update() {
    pos.add(direction);

    //add the speed difference for the next update
    direction.add(new Vector2(0, -acceleration));

    if (pos.y <= -TextureManager.DROPS.getHeight()) {
        float x = MathUtils.random(0, ManicMeltdowns.WIDTH - TextureManager.DROPS.getWidth());
        pos.set(x, ManicMeltdowns.HEIGHT);
    }

}

Then when creating just use a fixed speed.

\$\endgroup\$
1
  • \$\begingroup\$ Explaining the changes you've made would be helpful. \$\endgroup\$
    – House
    Feb 12, 2015 at 15:59

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .