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

2017/2/27

Timer doesn't work

JokeNotFound JokeNotFound

2017/2/27

#
Hey, so this is the code I have for now but it doesnt seem to work.
private int time = 0;
 
public WorldName()
{
    super(/**...*/);
    Greenfoot.setSpeed(50);
    prepare();
}
 
private void prepare()
{
    time = 3300;
    //adding objects here
}
 
public void act()
{
    time--;
    if (time == 0)
    {
        removeObjects(getObjects(null));
        prepare();
        /** OR **/
        Greenfoot.setWorld(new WorldName());
    }
}
The compiler says "invalid method declaration;return type required"
Nosson1459 Nosson1459

2017/2/27

#
You have to make sure that the name for the method on line 3 in the above code matches the name of the class that this method is in, since the names don't match you need a return type such as void, int, boolean, or double something like that. In the shown code it looks like it's supposed to be a class constructor method so change the name of the method to match the class name (and then you should also put the proper ints on line 5).
JokeNotFound JokeNotFound

2017/2/27

#
Ok so im not sure if I fully understand what you mean with proper ints on line 5. My knowledge about programming is very limited due to absence at school. I found this code in another program. Can you help me out? What do I need to enter to make the timer work? Thank you very much in advance for your help and time!!!
danpost danpost

2017/2/27

#
JokeNotFound wrote...
I found this code in another program.
If you found that code, as is, in another program, then that program did not work either. It looks to be like you found this in a discussion or tutorial. Line 5 is a World contructor call. See the greenfoot.World class API documentation on that method to see what is required in between the parenthesis. Also, you have a choice between using 21 through 22 or using line 24 -- you should not have both in your code (just lines 21 and 22 without 24 or just line 24 without lines 21 and 22). It using lines 21 and 22, add the following line with those two lines:
time = 3300;
When running, you will not see anything happen (you will not notice that the timer is decreasing or that the world is being reset, as is). For testing, you can add the following line as the first line in the act method:
if (time%55 == 0) showText(""+(time/55), 50, 50);
Nosson1459 Nosson1459

2017/2/28

#
JokeNotFound wrote...
Ok so im not sure if I fully understand what you mean with proper ints on line 5. My knowledge about programming is very limited due to absence at school. I found this code in another program. Can you help me out? What do I need to enter to make the timer work? Thank you very much in advance for your help and time!!!
You would need to enter something like
super(600, 400, 1);
as line 5 and you need to make sure that the method name on line 3 matches the class name. +
danpost wrote...
JokeNotFound wrote...
I found this code in another program.
If you found that code, as is, in another program, then that program did not work either. It looks to be like you found this in a discussion or tutorial. Line 5 is a World contructor call. See the greenfoot.World class API documentation on that method to see what is required in between the parenthesis. Also, you have a choice between using 21 through 22 or using line 24 -- you should not have both in your code (just lines 21 and 22 without 24 or just line 24 without lines 21 and 22). It using lines 21 and 22, add the following line with those two lines:
time = 3300;
When running, you will not see anything happen (you will not notice that the timer is decreasing or that the world is being reset, as is). For testing, you can add the following line as the first line in the act method:
if (time%55 == 0) showText(""+(time/55), 50, 50);
You need to login to post a reply.