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

2017/6/1

Spawn only if none are in the world?

alexaa alexaa

2017/6/1

#
Hello, I have some code here for my game. It spawns a random orb for random numbers. Sometimes it's great but sometimes it spawns multiple which are un-dodgable. Is there a way to make this code check if an orb is in the world already and if so don't spawn?
    public void randomSpawn() {
        if (Greenfoot.getRandomNumber(80) > 78)
        {
            Orb orb = new Orb();
            addObject(orb,526,513);
        }
    }

    public void waitForSpawning() {
        if(waitTime > 1500)
        { 
            randomSpawn();
            waitTime = 0;//resets timer 
        }
        waitTime ++;//timer counts up
    }
Super_Hippo Super_Hippo

2017/6/1

#
The following will return 'true' if no Orb is in the world. You can use it as another if condition.
getObjects(Orb.class).isEmpty()
You need to login to post a reply.