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

2021/3/12

How To Delay for a few Seconds

ConorK ConorK

2021/3/12

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Math here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Math extends Enemy
{
    /**
     * Act - do whatever the Math wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        // Add your action code here.
        problem();
    }
    
        
    public void problem()
    {
        int a = 0;
        
        while(a < 10)
        {
            
            setImage(new GreenfootImage("What is 2 x " + a , 50, Color.WHITE, Color.BLACK));
                        
            a++;
        }
        
    }
}
Whenever I start it up, "a" goes to 9 immediately. How can I make it so that a moves up a number every few seconds?
danpost danpost

2021/3/12

#
Changing "while" to "if" will limit it to one problem per act, instead of just jumping to the last one. You can either reduce the scenario speed or add a Greenfoot.delay(180) to act to put some time between each problem.
ConorK ConorK

2021/3/12

#
I've tried using the .delay() but I also have other objects moving at a certain speed, but they slow done when I use it. Is there a way to only delay the problem and not the entire screen?
danpost danpost

2021/3/12

#
ConorK wrote...
I've tried using the .delay() but I also have other objects moving at a certain speed, but they slow done when I use it. Is there a way to only delay the problem and not the entire screen?
Yes.. Add an int timer field. Increment it each act. Change problem when the timer is divisible by 180.
You need to login to post a reply.