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

2014/1/31

Replacing (creating) object when it's destroyed

csrgamer csrgamer

2014/1/31

#
What I'd like to do is have a new Food.class be created whenever the amount of Food.class on the screen is less than 1. Could someone help me out?
    public WombatWorld()
    {   
        super(800, 800, 1); 

        prepare();
        for (int i = 0; i < 1; i++)
        {
            int x = Greenfoot.getRandomNumber(getWidth() -60);
            int y = Greenfoot.getRandomNumber(getHeight() -60);
            addObject(new Food(), x, y);
        }
    }
danpost danpost

2014/1/31

#
There is no sense in using a for loop when then number of iterations is one. Your constructor, if corrected as is, should be like this instead:
public WombatWorld()
{
    super(800 800,, 1);
    
    prepare();

    int x = Greenfoot.getRandomNumber(getWidth()-60);
    int y = Greenfoot.getRandomNumber(getHeight()-60);
    addObject(new Food(), x, y);
}
This will add one worm into your world while the world is being constructed. This will not however, ever be executed for the same world more than once (a world is only constructed once). What you need is code that will continuously check for Food objects in the world, and, if none are found, to add one. The key word is 'continuously'. This is a job for the act method (a method that is called repeatedly by the system. The World class API shows that it also (as well as the Actor class) has an act method that is repeatedly called by the system while the world is active (and the scenario is running). This is where lines 7 through 9 above (in my constructor) should be placed, but it needs to be conditional executed. It needs to be in an 'if' block; and the condition would be:
if (getObjects(Food.class).isEmpty())
This is the 'whenever the amount of Food class...' (Food objects -- not class) 'on the screen' (rather, in the world) 'is less than 1' (is zero).
csrgamer csrgamer

2014/1/31

#
Oh I see, cool! Thanks for the help and explanation, I also have one more problem though. I thought putting the -60 would keep it from spawning within 60 pixels of the walls, but it doesn't seem to be working, as the food will sometime appear almost halfway into the wall. So anyways, thanks for the help, that part works perfectly now. And if anyone could shed some light on this, that'd be welcome as well!
danpost danpost

2014/1/31

#
csrgamer wrote...
I thought putting the -60 would keep it from spawning within 60 pixels of the walls, but it doesn't seem to be working, as the food will sometime appear almost halfway into the wall.
If you take notice, you will see that they appear almost halfway into the walls only on the top and left sides. The -60 only stops them from spawning near the right and bottom sides. To prevent spawning near any edge, consider this: you will be taking away 60 from both the top and bottom vertically and 60 from both the left and right horizontally. That is taking away 120 along each the horizontal and vertical. Therefore, you should be using -120, not -60. Still, the initial value both ways should be 60, not 0; we need to add 60 to both directions to shift the range of '0 to getWidth()-120' and '0 to getHeight()-120' to '60 to getWidth()-60' and '60 to getHeight()-60'. End result:
int x = 60+Greenfoot.getRandomNumber(getWidth()-120);
int y = 60+Greenfoot.getRandomNumber(getHeight()-120);
csrgamer csrgamer

2014/2/1

#
Alright that makes sense, thanks!
You need to login to post a reply.