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

2012/5/2

How can you let an actor counting down

1
2
3
lonely.girl lonely.girl

2013/1/9

#
hello there @danpost can you plz or anybody else comment the code above for me?? i.e timer class
danpost danpost

2013/1/9

#
@lonely.girl, hope this helps
import greenfoot.*;
import java.awt.Color;    

/**
 * an object that display the remaining time before the scenario stops
 */
public class Timer extends Actor   
{      
    long initialTime = System.currentTimeMillis(); // the start time
    int elapsedTime = 0; // to hold the elapsed time in milliseconds (1/1000th of a second)

    /*
     * sets the initial image of this Timer object
     */
    public Timer()   
    {
        updateImage(); // shows remaining time
    }  

    /*
     * runs the timer and updates the image of this Timer object
     */
    public void act()   
    {    
        elapsedTime = System.currentTimeMillis() - initialTime; // compute elapsed time
        updateImage(); // show the time remaining
        if (elapsedTime >= 60000) ((BackGround) getWorld()).gameOver(); // after 60 seconds, stop the scenario
    }
     
    /*
     * sets the image to the number of seconds remaining on the timer
     */
    private void updateImage()
    {
        setImage(new GreenfootImage("" + ((int) ((60000 - elapsedTime)) / 1000), 24, Colour.BLACK, new Color(0, 0, 0, 0)));
    }
}
lonely.girl lonely.girl

2013/1/9

#
Thx a lot, this is so helpful
13111876 13111876

2013/10/28

#
@danpost, I have used this code too, but the problem is the counter starts counting before I hit the ACT or RUN button!! For example my counter starts counting down from 90 seconds but when I press RUN it has already started. What do I need to change in the code below.
import greenfoot.*;
import java.awt.Color;    

/**
 * an object that display the remaining time before the scenario stops
 */
public class Timer extends Actor   
{      
    long initialTime = System.currentTimeMillis(); // the start time
    int elapsedTime = 0; // to hold the elapsed time in milliseconds (1/1000th of a second)

    /*
     * sets the initial image of this Timer object
     */
    public Timer()   
    {
        updateImage(); // shows remaining time
    }  

    /*
     * runs the timer and updates the image of this Timer object
     */
    public void act()   
    {    
        elapsedTime = System.currentTimeMillis() - initialTime; // compute elapsed time
        updateImage(); // show the time remaining
        if (elapsedTime >= 60000) ((BackGround) getWorld()).gameOver(); // after 60 seconds, stop the scenario
    }
     
    /*
     * sets the image to the number of seconds remaining on the timer
     */
    private void updateImage()
    {
        setImage(new GreenfootImage("" + ((int) ((60000 - elapsedTime)) / 1000), 24, Colour.BLACK, new Color(0, 0, 0, 0)));
    }
}
danpost danpost

2013/10/28

#
In the scenario this timer was created for, the timer object is not created until after the scenario is started. The timer runs on the system clock and starts as soon as you create the timer object (although the display is not updated until the scenario is started). You could add a method to 'reset' the timer, but might as well just replace the timer at the time you would reset it. Two ways you could solve your dilemma are (1) do not create the timer object and add it into the world until the scenario is started; or (2) instead of using the system clock, which is not consistent with runnings of the scenario, use frames (or act cycles) to mark the passage of 'time'. Using the latter method is probably the way to go. A scenario running at normal speeds would run about 50 to 60 frames (act cycles) per second (which equates to somewhere around 5000 frames in 90 seconds). The code for a class counting frames might be something like this:
import greenfoot.*;
import java.awt.Color;    

/**
 * an object that display the remaining time units before the scenario stops;
 * this class counts frames (or act cycles) as the regulating unit of time
 */
public class Timer extends Actor   
{      
    int time = 90; // seconds to run
    int fps = 55; // approximate number of frames per second (adjust as needed)
    int cycles = time * fps; // the countdown timer

    /*
     * sets the initial image of this Timer object
     */
    public Timer()   
    {
        updateImage(); // shows remaining time
    }  

    /*
     * runs the timer and updates the image of this Timer object
     */
    public void act()   
    {    
        cycles--; // decrement countdown timer
        if (cycles % fps == 0) updateImage(); // show the time remaining (only done when needed)
        if (cycles == 0) ((BackGround) getWorld()).gameOver(); // when countdown completes, stop the scenario
    }
     
    /*
     * sets the image to the number of approximate seconds remaining on the timer
     */
    private void updateImage()
    {
        setImage(new GreenfootImage("" + (cycles+fps-1)/fps, 24, Colour.BLACK, null));
    }
}
You need to login to post a reply.
1
2
3