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

2013/5/16

Timer

vbh vbh

2013/5/16

#
i want to start a timer when my game starts, and then count down to zero and end the game. My problem that i can see the text in world, but my timer dont count down?? From world class public class MyWorld extends World { private int timer = 6700; Text timerText = new Text(); public MyWorld() { // Create a new world with 600x400 cells with a cell size of 1x1 pixels. super(600, 400, 1); if (timer> 0) { timer--; if(timer%60==0) timerText.setTexi t("Time left: " +(timer/60)); if(timer == 0) Greenfoot.stop(); } //timeTimer(); addText(); } /** * Prepare the world for the start of the program. That is: create the initial * objects and add them to the world. */ //public void timeTimer() //{ //if (timer>0) // { // timer--; // if(timer == 0) Greenfoot.stop(); //} //} public void addText() { addObject(timerText, 100, 15); timerText.setText("Time left: " + (timer/60)); } } From TEXT.CLASS public class Text extends Actor { public Text() { this("Game over :("); } public Text(String text) { setText(text); } public void setText(String text) { setImage(new GreenfootImage(text, 24, Color.black, new Color(0, 0, 0, 0))); } }
Gevater_Tod4711 Gevater_Tod4711

2013/5/16

#
The problem is that your countdown is in the constructor of the world. So it is only executed when the world is built. You have to write the code for the timer (everything after super(...);) into the act method.
You need to login to post a reply.