Hi
Is it possible to create a timer that will stop the game after 60 seconds or a specified time limit.
If it is possible then how can i achieve this?


1 | private timer = 0 ; |
1 2 3 4 5 | if (timer> 0 ) { timer--; if (timer == 0 ) Greenfoot.stop(); } |
1 | private timer = 0 ; |
1 | int timer = 0 ; |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | import greenfoot.*; import java.awt.Color; public class Text extends Actor { public Text() { this ( "" ); } public Text(String text) { setText(text); } public void setText(String text) { setImage( new GreenfootImage(text, 24 , Color.black, new Color( 0 , 0 , 0 , 0 ))); } } |
1 2 3 4 5 6 7 | // add world instance field Text timerText = new Text(); // in the constructor (or a method it calls) addObject(timerText, 100 , 15 ); //wherever timerText.setText( "Time left: " + (timer/ 60 )); // insert after 'timer--;' in the 'act' method (or a method it calls) if (timer% 60 == 0 ) timerText.setText( "Time left: " + (timer/ 60 )); |
1 | Text timerText = new Text(); |
1 2 3 4 5 6 7 8 | public class ???????? extends World { Text timerText= new Text(); // other fields public ????????() { super (... // etc. |