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

2012/9/20

How to add object once condition is met?

ycart ycart

2012/9/20

#
Hi! For one of my assignments, I have to create a concept of levels in my game. But the levels don't change the world, it just adds another object (animal) into the game. So, I tried doing an if-statement like if (newLevel == 2) { getObjectblahblahblah } but when I do that, the method keeps running in a loop! So the object keeps adding more and more animals. How do I stop this loop? Or should I be using a boolean... Any suggestions would help a lot! Thanks! :)
MatheMagician MatheMagician

2012/9/20

#
when you say
newLevel=2
then you could just put all you getObjectblahblahblah code right after that and so when you change levels, the code will add and remove the necessary objects.
ycart ycart

2012/9/20

#
Hmm... I'm still lost! Let me put up the code that I have (but is running in a loop)
   private void levelUp () 
    {   
        if (currentLevel == 2) 
        {
           getWorld().addObject (new Dolphin (), Greenfoot.getRandomNumber (560), Greenfoot.getRandomNumber (560)); 
        }
    } 
danpost danpost

2012/9/20

#
We need to see the code that calls this method. Or maybe you need just to increment 'currentLevel' within this method. instead of possibly elsewhere.
ycart ycart

2012/9/20

#
the code that calls the current level method? ah! all of this new terminology! haha, sorry i'm new at this!
    private void eatWorm ()
    {
        if(canSee(Worm.class))
        { 
            eat(Worm.class);
            wormsEaten = wormsEaten + 1;
            currentLevel = wormsEaten/2 + 1; 
        } 
hope this was what you needed to see!
ycart ycart

2012/9/20

#
So whenever my crab eats two worms, it goes to "level 2" but then when I use the if-statement, it keeps performing the method and adding more dolphins when I only need one for every increment in levels.
danpost danpost

2012/9/20

#
I do not see the statement
levelUp();
within the 'eatWorm()' method.
danpost danpost

2012/9/20

#
You could change line 7 in the 'eatWorm()' method to 'levelUp();' and for the 'levelUp()' method, use the following:
private void levelUp () 
{   
    if (currentLevel != wormsEaten / 2 + 1) 
    {
        getWorld().addObject (new Dolphin (), Greenfoot.getRandomNumber (560), Greenfoot.getRandomNumber(560));
        currentLevel = wormsEaten / 2 + 1;
    }
}
danpost danpost

2012/9/20

#
Another way to handle this is with the following statement within the act() method after you call 'eatWorm();', and the 'currentLevel' field is not needed at all:
if (getWorld().getObjects(Dolphin.class).size() < wormsEaten / 2 + 1) addObject(new Dolphin(), Greenfoot.getRandomNumber(560), Greenfoot.getRandomNumber(560));
This statement may need adjusted (as I do not know how many dolphins you start with -- this assumes zero). One thing, you said for every two worms eaten, a new dolphin is added. However with the '+1', you are adding a new dolphin at values of 1, 3, 5, 7... instead of 2, 4, 6, 8 ... If that was intentional, then fine; if not, just remove the '+1's.
ycart ycart

2012/9/20

#
Thanks danpost! I'll try it out right now!
danpost danpost

2012/9/20

#
Another way (without the 'currentLevel' field) is with this condition
if (wormsEaten % 2 == 1) getWorld().addObject(new Dolphin...
Change '1' to '0' if using 2, 4, 6... instead of 1, 3, 5... This statement would be in the 'levelUp' method or right after the statement 'wormsEaten = wormsEaten + 1;' in the 'eatWorm()' method (instead of calling 'levelUp();').
ycart ycart

2012/9/20

#
Ok, awesome! I got it work after making some few adjustments and to make my code cleaner, thanks!
You need to login to post a reply.