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

2014/1/10

Timer!

Lime25Sour Lime25Sour

2014/1/10

#
i would like it so after so many seconds you win! im not sure how to do this please help! additional info: would like the timer to be shown in the corner of the screen and to flash up that you win
danpost danpost

2014/1/10

#
I just did something similar to this in another discussion. I suggested counting act cycles to regulate the clock timer. I said a scenario running at normal speeds run about 50 to 60 act cycles per second; so multiply 50 by the number of seconds till the game is won to get the number of act cycles that must pass. You will need an instance int field to count the act cycles.
1
2
3
4
5
6
7
8
9
// add instance field
private int actCounter;
// in act method
runActCounter();
// add method
private void runActCounter()
{
    if (++actCounter == 50 /*  times seconds  */) Greenfoot.stop();
}
Lime25Sour Lime25Sour

2014/1/11

#
thx is there a way to show how much time you have left in the corner of the screen?
Lime25Sour Lime25Sour

2014/1/12

#
Dont worry now. I did some editing and heres the finished code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;
/**
 * Times the game so after so many minutes you win!
 *
 * @author Lime25Sour & Danpost
 * @version (a version number or a date)
 */
public class Timer extends Actor
{
    private int actCounter;
    Color[] colors = { Color.GREEN, Color.BLUE };
    int colorNum = 0;
    int counter = 1;
     
    /**
     * Act - do whatever the Timer wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
        runActCounter();
    }   
     
    /**
     * After 1 and a half minutes (50acts*No.ofSeconds) the method winner is called.
     */
    private void runActCounter() 
    
        if (++actCounter == 4500 /*  times seconds  */) Winner();
    }
    
    /**
     * Plays the winner sound, calls the ShowWin method then stops Greenfoot.
     */
    public void Winner()
    {
       Greenfoot.playSound("Winner.wav");
       ShowWin();
       Greenfoot.stop();
    }
     
    /**
     * Puts the words 'YOU WIN!' on the screen in green.
     */
      public void ShowWin()
    {
        setImage(new GreenfootImage("YOU\nWIN!", 96, colors[colorNum], new Color(0, 0, 0, 0)));
    }
}
You need to login to post a reply.