gafflx February 2016

Spawn balls randomly

Im making a game where balls spawn randomly from each side of the smartphone screen, and I got it working although the balls spawn mostly on the top side of the screen and 2/10 times on the bottom and sides of the screen. Heres the code:

//creates the balls and sets their position as well as the random timer for each
private void spawnBalls1() {

    Rectangle ball1 = new Rectangle();
    ball1.x = MathUtils.random(639, 641);
    ball1.y = 720;
    ball1.width = 32;
    ball1.height = 32;
    balls1.add(ball1);
    lastDropTime = TimeUtils.nanoTime();

}

private void spawnBalls2() {

    Rectangle ball2 = new Rectangle();
    ball2.x = 0;
    ball2.y = MathUtils.random(359, 361);
    ball2.width = 32;
    ball2.height = 32;
    balls2.add(ball2);
    lastDropTime = TimeUtils.nanoTime();
}

private void spawnBalls3() {

    Rectangle ball3 = new Rectangle();
    ball3.x = MathUtils.random(639, 641);
    ball3.y = 0;
    ball3.width = 32;
    ball3.height = 32;
    balls3.add(ball3);
    lastDropTime = TimeUtils.nanoTime();
}

private void spawnBalls4() {

    Rectangle ball4 = new Rectangle();
    ball4.x = 1280;
    ball4.y = MathUtils.random(359, 361);
    ball4.width = 32;
    ball4.height = 32;
    balls4.add(ball4);
    lastDropTime = TimeUtils.nanoTime();
}


//draws the balls
    for (Rectangle ball1 : balls1) {

        batch.draw(Ball, ball1.x, ball1.y);
    }

    for (Rectangle ball2 : balls2) {

        batch.draw(Ball, ball2.x, ball2.y);
    }

    for (Rectangle ball3 : balls3) {

        batch.draw(Ball, ball3.x, ball3.y);
    }

    for (Rectangle ball4 : balls4) {

        batch.draw(Ball, ball4.x, ball4.y);
    }


// if the time minus the time of the last ball spawn is less than x then spawn another ball in a random place
    if (TimeUtils.nanoTime() - lastDropTime > 1000000000) spawnBalls1();

    Iterator<Rectangle> iter1 = balls1.iterator();
    while(iter1.hasNext()) {

        Rectangle balls1 = iter1.next();
             

Answers


S.Klumpers February 2016

Is this what you are looking for?

private void spawnBalls1() {

    Rectangle ball1 = new Rectangle();
    ball1.x = MathUtils.random(0, 641);
    ball1.y = MathUtils.random(0, 720);
    ball1.width = 32;
    ball1.height = 32;
    balls1.add(ball1);
    lastDropTime = TimeUtils.nanoTime();

}


Tenfour04 February 2016

All four of your ball spawning methods share the same lastDropTime variable. The execution time between

if (TimeUtils.nanoTime() - lastDropTime > 1000000000) spawnBalls1();

and

if (TimeUtils.nanoTime() - lastDropTime > 1000000000) spawnBalls4();

is going to be a tiny percentage of 1000000000 ns, so it's going to be very unlikely for spawnBalls2-4 ever to get called.

You should only have one test instead of four, and randomly pick one of the methods. Something like this:

if (TimeUtils.nanoTime() - lastDropTime > 1000000000){
    switch (MathUtils.randomInt(4)){
        case 0: spawnBalls1(); break;
        case 1: spawnBalls2(); break;
        case 2: spawnBalls3(); break;
        case 3: spawnBalls4(); break;
    }
}

Also, this is a lot of copy-paste code, which is ballooning your time investment. You ought to write one spawnBalls method that takes some parameter that affects where it places the balls.

Post Status

Asked in February 2016
Viewed 1,685 times
Voted 12
Answered 2 times

Search




Leave an answer