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

2015/3/28

Adding clock

Ankit_Thapa Ankit_Thapa

2015/3/28

#
Can I add timer to my greenfoot project which will display how long have been the player playing the game? If yes how can I do so?
danpost danpost

2015/3/28

#
Create a subclass of Actor. Add a field to hold the timer value. Add a constructor to set the initial image of the actor. Have the act method adjust the timer value and update its image as needed. Helpful hint: since the constructor and the act method both require that the image of the actor be updated, you can put the code to set the image in a separate method and call it from both those locations. For example of a sample layout:
import greenfoot.*;

public class Timer extends Actor
{
    private int time;

    public Timer()
    {
        updateImage();
    }

    public void act()
    {
        // adjust time value here
        if (/** needed */) updateImage();
    }

    private void updateImage()
    {
        // set image
    }
}
After completing the class, create a Timer object (using 'new Timer()') and add it into the world.
Ankit_Thapa Ankit_Thapa

2015/3/28

#
Can you provide me full code for this?
danpost danpost

2015/3/28

#
Ankit_Thapa wrote...
Can you provide me full code for this?
That would be difficult not knowing more about how you want the timer to work. The things I used comments for are all things that are variable -- things that depend on how you want it to perform: - what timing system you wish to use - what conditions you would prefer to impose for updating the image - how you want the image displayed
Ankit_Thapa Ankit_Thapa

2015/3/28

#
Basically I want to start the timer whenever the user press the run button. I want to display the timer till the game ends
danpost danpost

2015/3/28

#
Ankit_Thapa wrote...
Basically I want to start the timer whenever the user press the run button. I want to display the timer till the game ends
"Basically" is not enough. You need to be detailed and explicit; and provide all aspects of how you want it.
You need to login to post a reply.