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

2012/9/30

"while" doesn't work

luna124 luna124

2012/9/30

#
public void populateWorld()
    {
        addObject(new Crab(),Greenfoot.getRandomNumber(560),Greenfoot.getRandomNumber(560));
        
        addObject(new Lobster(),Greenfoot.getRandomNumber(560),Greenfoot.getRandomNumber(560));
        addObject(new Lobster(),Greenfoot.getRandomNumber(560),Greenfoot.getRandomNumber(560));
        addObject(new Lobster(),Greenfoot.getRandomNumber(560),Greenfoot.getRandomNumber(560));
            
        addObject(new Worm(),Greenfoot.getRandomNumber(560),Greenfoot.getRandomNumber(560));
        addObject(new Worm(),Greenfoot.getRandomNumber(560),Greenfoot.getRandomNumber(560));
        addObject(new Worm(),Greenfoot.getRandomNumber(560),Greenfoot.getRandomNumber(560));
        addObject(new Worm(),Greenfoot.getRandomNumber(560),Greenfoot.getRandomNumber(560));
        addObject(new Worm(),Greenfoot.getRandomNumber(560),Greenfoot.getRandomNumber(560));
        addObject(new Worm(),Greenfoot.getRandomNumber(560),Greenfoot.getRandomNumber(560));
        addObject(new Worm(),Greenfoot.getRandomNumber(560),Greenfoot.getRandomNumber(560));
        addObject(new Worm(),Greenfoot.getRandomNumber(560),Greenfoot.getRandomNumber(560));
        addObject(new Worm(),Greenfoot.getRandomNumber(560),Greenfoot.getRandomNumber(560));
        addObject(new Worm(),Greenfoot.getRandomNumber(560),Greenfoot.getRandomNumber(560));    
    }
Hi together! our teacher told us to write this code in a while-loop (I don't know if this is the correct word in English). I did it this way and there are no errors, but if I compile there is no image, it's just white.
  public void populateWorld()
    {
        addObject(new Crab(),Greenfoot.getRandomNumber(560),Greenfoot.getRandomNumber(560));
        
        int i=0;
        while (i<3)
        {
            addObject(new Lobster(),Greenfoot.getRandomNumber(560),Greenfoot.getRandomNumber(560));
        }
  
            
        int j=0;
        while (j<10)
        {
            addObject(new Worm(),Greenfoot.getRandomNumber(560),Greenfoot.getRandomNumber(560));
        }   
    }
Surely, I did a mistake somewhere but I don't find it. Can you please help me? Thanks, luna124
Gevater_Tod4711 Gevater_Tod4711

2012/9/30

#
the problem is in the loop. If i is 0 the loop is executed and if i is not countet up i will be 0 for any time so you got a continuous loop which Greenfoot doesn't like. The act will never end and so there will never be anything to see on your screen. So either do this:
int i = 0;
while (i < 3) {
    getWorld().addObject(new Lobster(), Greenfoot.getRandomNumber(560), Greenfoot.getRandomNumber(560));  //only use getWorld() if this is on an actor class;
    i++;
}
or what would be a better idea: use a for loop:
for (int i = 0; i < 3; i++) {
    addObject(new Lobster(), Greenfoot.getRandomNumber(560), Greenfoot.getRandomNumber(560));
}
the for loop does the same but it was only created for this cases where you need to count variables.
luna124 luna124

2012/9/30

#
//only use getWorld() if this is on an actor class; i++;
What do you mean with that? I'm a total beginner with greenfoot and we didn't talk about getWorld() at school :-(
Gevater_Tod4711 Gevater_Tod4711

2012/9/30

#
if you write this in a class which extends World you just have to write addObject(new ... but if you write it in a class which extends Actor you have to write getWorld().addObject(new ...
luna124 luna124

2012/9/30

#
It's in the constructor of CrabWorld. But both codes don't work if I write them in my code as you said...
davmac davmac

2012/9/30

#
luna124, it should work, but you might need to re-start Greenfoot. If you do that and it still doesn't work, please post your current code again. :)
luna124 luna124

2012/9/30

#
Oh, sorry, I forgot to restart it. It works now. Sorry.... And thank you
You need to login to post a reply.