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

2013/5/24

How do you make a sequence of objects appear?

johnguackmbl johnguackmbl

2013/5/24

#
for(int i = 0; i < 10; i++)
{
     addObject(new Block(),(100+10*i),100);
     //add something?
}
So I was wondering if I could add anything in the for-loop to make the loop "slow down" in order to display the blocks in a sequence. Any suggestions? Please?
danpost danpost

2013/5/24

#
If there is nothing else that needs to be happening when this code is executing, which I believe would be the case here, you could paint the world and add a delay within the loop. I will presume the code is in a world class.
for (int i=0; i<10; i++)
{
    addObject(new Block(), (100+10*i), 100);
    repaint();
    Greenfoot.delay(15);
}
johnguackmbl johnguackmbl

2013/5/24

#
danpost wrote...
If there is nothing else that needs to be happening when this code is executing, which I believe would be the case here, you could paint the world and add a delay within the loop. I will presume the code is in a world class.
for (int i=0; i<10; i++)
{
    addObject(new Block(), (100+10*i), 100);
    repaint();
    Greenfoot.delay(15);
}
Hi, danpost. What does repaint do exactly? And could I use the Greenfoot.delay alone? Thanks for helping!
danpost danpost

2013/5/24

#
I am not sure if Greenfoot will repaint (update the display) just by using 'delay' alone.
johnguackmbl johnguackmbl

2013/5/24

#
danpost wrote...
I am not sure if Greenfoot will repaint (update the display) just by using 'delay' alone.
Hey danpost. When I add the repaint() method, and then I click compile, nothing shows up. Like, just a gray screen is shown, exactly like there's no world at all. The buttons are all grayed out, too. When I remove repaint() and restart Greenfoot, the world is fine again. Also, Greenfoot.delay() doesn't work by itself. Please help! Thanks!
danpost danpost

2013/5/24

#
The code cannot be placed in the world constructor or a method it calls. If you need this during the construction of the world, put it in a 'public void started()' method. You may want to add an instance boolean field to the world class to determine whether it was done or not (so stopping and restarting the scenario does not run the code again. Oh, and I check it out -- repainting is automatically done when 'delay' is called.
// add instance field to world class
private Boolean hasStarted;
// create a 'started' method
public void started()
{
    for (int i=0; i<10; i++)
    {
        addObject(new Block(), (100+10*i), 100);
        Greenfoot.delay(15);
    }
}
johnguackmbl johnguackmbl

2013/5/25

#
danpost wrote...
The code cannot be placed in the world constructor or a method it calls. If you need this during the construction of the world, put it in a 'public void started()' method. You may want to add an instance boolean field to the world class to determine whether it was done or not (so stopping and restarting the scenario does not run the code again. Oh, and I check it out -- repainting is automatically done when 'delay' is called.
// add instance field to world class
private Boolean hasStarted;
// create a 'started' method
public void started()
{
    for (int i=0; i<10; i++)
    {
        addObject(new Block(), (100+10*i), 100);
        Greenfoot.delay(15);
    }
}
Thanks, danpost. That worked. Do you know how long 1 time step is for the Greenfoot.delay() method?
danpost danpost

2013/5/25

#
It varies; but a general rule for a scenario running at standard speed (speed slider in the middle) is somewhere around 55 to 60 cycles per second.
You need to login to post a reply.