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

2011/7/22

Creating objects at random intervals

kabads kabads

2011/7/22

#
I would like plants to appear randomly during the game as a health thing for an animal (to help them survive). However, I put the code in the World Class where a random number is created and then checked to see if it is equal to 1. If it is then addPlant(); The problem is that this leads to an infinite loop which stops the world being generated. Where is the best place to put code like this that allows the world to generate random plants? TIA Kabads
GameCode GameCode

2011/7/22

#
So, your problem is that the World adds too much plants in a too short period of time? What is the limit of your random numbers? Maybe you could use this:
public class ....
{
    public int a = 0;
    
    public void act()
    {
        a++;
        If(a == FOR EXAMPLE   100)
        {
             RandomThingMethod();
        }
    }
    public void RandomThingMethod()
    {
        int z = Greenfoot.getRandomNumber(10);
        int x = Greenfoot.getRandomNumber(getWidth());
        int y = Greenfoot.getRandomNumber(getHeight);
        if(z == 1)
            addObject(new Plant(),x,y)
    }
}
kabads kabads

2011/7/22

#
No, my problem is that the instatiation of the world never gets off the ground, as the loop is constantly checking whether it should create a plant. I never see any object of the worlds created.
GameCode GameCode

2011/7/22

#
Are you sure you wrote your code in the act-Method??? Maybe you wrote it into a while- or for- loop
kabads kabads

2011/7/23

#
GameCode: I didn't put it in the act method because there is no initial instantiation of the plant to act upon. I put a while loop in the constructor of the World Class which creates random numbers to check if a plant should be instantiated. But this loop prevents the World from being created as it is infinite (I want it to run through the whole game). import greenfoot.*; // imports Actor, World, Greenfoot, GreenfootImage import java.util.Random; public class CrabWorld extends World { /** * Create the crab world (the beach). Our world has a size * of 560x460 cells, where every cell is just 1 pixel. */ int i=0; public CrabWorld() { super(560, 460, 1); setBackground("sand.jpg"); populate(); while(i==0){ int checkplant = Greenfoot.getRandomNumber(500); if (checkplant ==1){ addSeaweed(); } } } public void populate(){ addObject(new Crab(), Greenfoot.getRandomNumber(500),Greenfoot.getRandomNumber(400)); addObject(new Lobster(), Greenfoot.getRandomNumber(500),Greenfoot.getRandomNumber(400)); addObject(new Lobster(), Greenfoot.getRandomNumber(500),Greenfoot.getRandomNumber(400)); addObject(new Lobster(), Greenfoot.getRandomNumber(500),Greenfoot.getRandomNumber(400)); addObject(new Lobster(), Greenfoot.getRandomNumber(500),Greenfoot.getRandomNumber(400)); addObject(new Lobster(), Greenfoot.getRandomNumber(500),Greenfoot.getRandomNumber(400)); addObject(new Lobster(), Greenfoot.getRandomNumber(500),Greenfoot.getRandomNumber(400)); addObject(new Lobster(), Greenfoot.getRandomNumber(500),Greenfoot.getRandomNumber(400)); addObject(new Lobster(), Greenfoot.getRandomNumber(500),Greenfoot.getRandomNumber(400)); addObject(new Lobster(), Greenfoot.getRandomNumber(500),Greenfoot.getRandomNumber(400)); addObject(new Lobster(), Greenfoot.getRandomNumber(500),Greenfoot.getRandomNumber(400)); } public void addSeaweed(){ addObject(new Seaweed(), Greenfoot.getRandomNumber(500),Greenfoot.getRandomNumber(400)); } }
kabads kabads

2011/7/23

#
OK - I have it now - the CrabWorld can have an act() method too - thanks guys.
DonaldDuck DonaldDuck

2011/7/23

#
GameCode wrote...
Maybe you could use this:
public class ....
{
    public int a = 0;
    
    public void act()
    {
        a++;
        If(a == FOR EXAMPLE   100)
        {
             RandomThingMethod();
        }
    }
    public void RandomThingMethod()
    {
        int z = Greenfoot.getRandomNumber(10);
        int x = Greenfoot.getRandomNumber(getWidth());
        int y = Greenfoot.getRandomNumber(getHeight);
        if(z == 1)
            addObject(new Plant(),x,y)
    }
}
I'll just fix some minor problems with GameCodes code and incorporate your code to set up the World...
import greenfoot.*;

public class CrabWorld extends World
{
    public int a = 0;
    private int threshold = 100; //increase to make the time until a random thing is added longer...

    public CrabWorld()
    {
        super(560, 460, 1);
        setBackground("sand.jpg");
        populate();
    }
    
    public void populate()
    {
        addObject(new Crab(), Greenfoot.getRandomNumber(500),Greenfoot.getRandomNumber(400));

        addObject(new Lobster(), Greenfoot.getRandomNumber(500),Greenfoot.getRandomNumber(400));
        addObject(new Lobster(), Greenfoot.getRandomNumber(500),Greenfoot.getRandomNumber(400));
        addObject(new Lobster(), Greenfoot.getRandomNumber(500),Greenfoot.getRandomNumber(400));
        addObject(new Lobster(), Greenfoot.getRandomNumber(500),Greenfoot.getRandomNumber(400));
        addObject(new Lobster(), Greenfoot.getRandomNumber(500),Greenfoot.getRandomNumber(400));
        addObject(new Lobster(), Greenfoot.getRandomNumber(500),Greenfoot.getRandomNumber(400));
        addObject(new Lobster(), Greenfoot.getRandomNumber(500),Greenfoot.getRandomNumber(400));
        addObject(new Lobster(), Greenfoot.getRandomNumber(500),Greenfoot.getRandomNumber(400));
        addObject(new Lobster(), Greenfoot.getRandomNumber(500),Greenfoot.getRandomNumber(400));
        addObject(new Lobster(), Greenfoot.getRandomNumber(500),Greenfoot.getRandomNumber(400));
    }

    public void act()
    {
        a++;
        if(a == threshold)
        {
            RandomThingMethod();
            a = 0;
        }
    }

    public void RandomThingMethod()
    {
        int z = Greenfoot.getRandomNumber(10);
        int x = Greenfoot.getRandomNumber(getWidth());
        int y = Greenfoot.getRandomNumber(getHeight());

        if(z == 1) //delete or comment out this line to make it always add after (threshold) game ticks
            addObject(new Seaweed(),x,y) //keep in mind that it's decided by random if it adds it or not.
    }
}
It should be directly copy-over-able now. Credits to GameCode, of course, for the random thing code.
CodingIssues CodingIssues

2016/7/2

#
I am trying to make a game that when you touch the heart your live increases by one, and thy lives pop out randomly but somethings say illegal public int a = 0 public void act() { a++; If(a == FOREXAMPLE 100) } { RandomThingMethod(); } } public void RandomThingMethod() { int z = Greenfoot.getRandomNumber(10); int x = Greenfoot.getRandomNumber(getWidth()); int y = Greenfoot.getRandomNumber(getHeight); if(z == 1) addObject(new Lives(),x,y) } } } }
danpost danpost

2016/7/3

#
@CodingIssues, you should start a new discussion about your issues; however, I can say the you have an excess number of squiggly brackets. Remove the first two (a closing and an opening one) after the 'if' statement and remove the last three given in your code above. Another issue will be that 'a' will only be '100' once and if the random number does not come up '1' at that one time, no Lives object will ever spawn. You should probably remove one of the two 'if' conditions for spawning a Lives object. Removing the first one will probably cause too many of them to be spawned and the random range for 'z' will need to increase substantially; removing the second will cause exactly one to be spawned about two seconds after the actor begins acting. One condition you may want to add is one the determines that no Lives object is already in the world, so no more than one will be in the world at a time. There is a way that you could maintain both 'if' statements in the code and that is if you reset 'a' back to zero in the 'RandomThingMethod' method (regardless of whether a Lives object is spawned or not.
You need to login to post a reply.