1
\$\begingroup\$

I need to make that objects fall from the top of the screen random intervals and positions. Here's my code where I create lot of objects and add them to ArrayList

private void createBalls() {
        BodyDef ballBodyDef = new BodyDef();
        ballBodyDef.type = BodyDef.BodyType.DynamicBody;

        CircleShape shape = new CircleShape();
        shape.setRadius(10/Constants.PPM);
        shape.setPosition(new Vector2(w/4/ Constants.PPM, 5));

        FixtureDef fd = new FixtureDef();
        fd.density = 1;
        fd.friction = 0.5f;
        fd.restitution = 0.5f;
        fd.shape = shape;

        do {
            float rand = MathUtils.random(-2.0f, 2f);
            shape.setPosition(new Vector2(w/4/ Constants.PPM + rand, 5));
            fd.shape = shape;

            bodyArrayList.add(world.createBody(ballBodyDef));
            bodyArrayList.get(bodyArrayList.size() - 1).createFixture(fd);
        } while (bodyArrayList.size() < 100);

        shape.dispose();
    }

this code works fine (except that all objects fall at single time). So is it possible to make that objects would fall at different time?

\$\endgroup\$
1
  • \$\begingroup\$ what is that w? shape.setPosition(new Vector2(w / 4 / Constants.PPM, MathUtils.random(-100f, 5f)); \$\endgroup\$
    – Alf Pacino
    Commented Dec 2, 2016 at 19:26

1 Answer 1

1
\$\begingroup\$

You can call the createBalls at some interval from your game loop, and possibly pass in how many balls you want it to create.

For example; public class YourGame extends Game {

    private long lastTime;

    @Override
    public void create() {
    }

    @Override
        public void render() {
        if (TimeUtils.nanoTime() - lastTime; > 1000000000) createBalls(10);

        // Render the world
    }

    private void createBalls(int ballCount) {
        BodyDef ballBodyDef = new BodyDef();
        ballBodyDef.type = BodyDef.BodyType.DynamicBody;

        CircleShape shape = new CircleShape();
        shape.setRadius(10 / Constants.PPM);
        shape.setPosition(new Vector2(w / 4 / Constants.PPM, 5));

        FixtureDef fd = new FixtureDef();
        fd.density = 1;
        fd.friction = 0.5f;
        fd.restitution = 0.5f;
        fd.shape = shape;

        for (int i = 0; i < ballCount; ++i) {
            float rand = MathUtils.random(-2.0f, 2f);
            shape.setPosition(new Vector2(w / 4 / Constants.PPM + rand, 5));
            fd.shape = shape;

            Body body = world.createBody(ballBodyDef));
            body.createFixture(fd);
        }

        shape.dispose();
        lastDropTime = TimeUtils.nanoTime();
    }
}

Alternatively, if you just want 100 balls created once and then have them fall you could create them with a random y value in the position that extends beyond the screen. That would make the balls seem to fall at different times;

private void createBalls() {
    BodyDef ballBodyDef = new BodyDef();
    ballBodyDef.type = BodyDef.BodyType.DynamicBody;

    CircleShape shape = new CircleShape();
    shape.setRadius(10 / Constants.PPM);
    shape.setPosition(new Vector2(w / 4 / Constants.PPM, MathUtils.random(-100f, 5f));

    FixtureDef fd = new FixtureDef();
    fd.density = 1;
    fd.friction = 0.5f;
    fd.restitution = 0.5f;
    fd.shape = shape;

    do {
        float rand = MathUtils.random(-2.0f, 2f);
        shape.setPosition(new Vector2(w / 4 / Constants.PPM + rand, 5));
        fd.shape = shape;

        bodyArrayList.add(world.createBody(ballBodyDef));
        bodyArrayList.get(bodyArrayList.size() - 1).createFixture(fd);
    } while (bodyArrayList.size() < 100);

    shape.dispose();
}
\$\endgroup\$

You must log in to answer this question.

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