This site requires JavaScript, please enable it in your browser!
Greenfoot back
Starlord_20
Starlord_20 wrote ...

2020/3/27

Create objects at random intervalls

Starlord_20 Starlord_20

2020/3/27

#
I need a method that adds a given count of objects in random intervalls. How could I do that without "stopping" the world (wait(), delay() ...)
danpost danpost

2020/3/27

#
Starlord_20 wrote...
I need a method that adds a given count of objects in random intervalls. How could I do that without "stopping" the world (wait(), delay() ...)
Use an int counter along with an act method in your World subclass.
Starlord_20 Starlord_20

2020/3/27

#
And how can I do that randomly?
danpost danpost

2020/3/27

#
Starlord_20 wrote...
And how can I do that randomly?
By setting the counter to a minimum value plus some random value when spawning. Decrease the counter and when zero spawn objects.
Starlord_20 Starlord_20

2020/3/28

#
It doesn`t really work.... here is my code (in World) :
public void act() {
        int scrollAmt = 1;
        GreenfootImage bg = new GreenfootImage(getBackground());


        getBackground().drawImage(bg, 0, scrollAmt); 
        getBackground().drawImage(bg, 0, scrollAmt-getHeight());
        if(lvlPassed()) {
            int i = 0;
            while(i < lvl) {
                int rdm = Greenfoot.getRandomNumber(100)+100;
                while(rdm != 0) rdm --;
                Asteroid ast = new Asteroid(lvl);    
                addObject(ast,Greenfoot.getRandomNumber(getWidth()),0);
                i++;
            }
            level.nextLVL();
        }

    }
now, every Asteroid spawns nearly at the same time
Super_Hippo Super_Hippo

2020/3/28

#
This line
while(rdm != 0) rdm --;
is essentially the same as
rdm = 0;
(Unless when rdm is negative, then it will create an infinite loop. But that won‘t happen in your case of course.) Maybe this can guide you in the correct direction.
Starlord_20 Starlord_20

2020/3/28

#
So what should I do instead?
danpost danpost

2020/3/28

#
Control must be passed back to greenfoot for all objects to complete their actions for an act cycle to complete. Any and all loops must be exited from for that to happen. That is why everything happened at once (in one act frame). A counter cannot be maintained from one act to the next when declared inside of a method. Its scope is too local (the scope is determined by the closest pair of squiggly brackets that contain the variable). Your counter needs to be declared outside the method so that the class brackets are the closest containing ones. After that, avoid using loops here.
Starlord_20 Starlord_20

2020/3/28

#
I am still attached to it... If the boolean lvlPassed() is true, every Asteroid must be added before lvlPassed() gets called again and after that, the addLevel() method must be called once. I tried many different ways to do that without a loop but it didn't work...
danpost danpost

2020/3/28

#
Again, you need a spawn timer to be outside the method. You will also need a spawn counter:
private int spawnTimer;
private int spawnCounter;
In act, if timer is zero and counter is less than level, spawn, increment counter and set timer:
if (spawnTimer == 0 && spawnCounter < lvl)
{
    // add asteroid
    spawnCounter++;
    spawnTimer = 100+Greenfoot.getRandomNumber(100);
} else spawnTimer--;
danpost danpost

2020/3/28

#
The check to level up can be:
if (spawnCounter == lvl && getObjects(Asteroid.class).isEmpty()) level.nextLVL();
Starlord_20 Starlord_20

2020/3/28

#
The int spawnTimer got negative because the Asteroid was still there, so I added a condition which kept the int on 0 and then it worked. thanks @danpost and @Super_Hippo!
You need to login to post a reply.