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

2012/3/19

Pre-Game Countdown

JayWood860 JayWood860

2012/3/19

#
i would like to place an actor in my world that'll countdown from 3 when the world starts and then my game plays. i want to do this so that when you press run, the game doesn't start automatically (as it is easy to die in my game). i've thought about doing things like time--, but i don't know how i would add this to my code. help please?
Duta Duta

2012/3/19

#
It depends if you want it in terms of seconds or act() methods.
JayWood860 JayWood860

2012/3/20

#
seconds
danpost danpost

2012/3/20

#
The following code will count a 3-second interval of time. However, it will also consume all of your CPU for those 3 seconds. You could try changing ';' to 'delay(1);', but I do not think the results will be adequate.
long startTime = System.currentTimeMillis();
while (startTime + 3000 < System.currentTimeMillis());
JayWood860 JayWood860

2012/3/20

#
this isn't working for me. here is the code im using:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Space here.
 * 
 * @Juwan Wood 
 * @Project Bravo
 */
public class Space extends World
{
    private Player1 P1;
    Score scoreCounter = new Score("Score: ");
    GameOver got = new GameOver();
    private People actor;
    boolean chosen;
    boolean ready = false;
    /**
     * Constructor for objects of class Space.
     * 
     */
    public Space()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(1000, 500, 1); 
        
        choosePlayer();
    }
    public void act()
    {
        if ( chosen == true )
        {
            getReady();
        }
        if ( ready == true )
        {
            reset();
        }
    }
    public void reset()
    {        
        addObject(actor, 500, 200);
        addRocks();
        addObject(scoreCounter,(getHeight() / 2), 480);    
        music.playLoop(); 
        ready = false;
    }
    public void choosePlayer()
    {
        addObject(new Stewie(), 330, 250);
        addObject(new Homer(), 660, 250);
    }
    public void chosenStewie()
    {
        actor = new Player1();
        chosen = true;
    }
    public void chosenHomer()
    {
        actor = new Player2();
        chosen = true;
    }
    public void getReady()
    {
        chosen = false;
        if (ready == false)
        {
            long startTime = System.currentTimeMillis();  
            while (startTime + 3000 < System.currentTimeMillis()); 
            ready = true;
        }
    }
}
there are no syntax errors nor does the terminal window appear. everything continues to work but there is no wait.
danpost danpost

2012/3/20

#
You may have to enclose everything in all actor's act() methods with an 'if (!((Space) getWorld()).delaying)', and add the public boolean 'delaying' variable in the world class, setting it true while counting down the 3 seconds, otherwise false.
JayWood860 JayWood860

2012/3/25

#
danpost wrote...
You may have to enclose everything in all actor's act() methods with an 'if (!((Space) getWorld()).delaying)', and add the public boolean 'delaying' variable in the world class, setting it true while counting down the 3 seconds, otherwise false.
ive done this all except for the last part, where and when do change the value of this boolean?
Duta Duta

2012/3/25

#
Oh by the way, I posted a scenario that might help out here: http://www.greenfoot.org/scenarios/4673
JayWood860 JayWood860

2012/3/25

#
this looks like it will help thanks
danpost danpost

2012/3/25

#
Set it to 'true' immediately before the two lines provided for the countdown above. Set it to 'false' immediately after the two lines provided for the countdown above.
You need to login to post a reply.