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

2012/3/12

counter

1
2
joemoma joemoma

2012/3/12

#
how do you creat a counter that counts down from 30 seconds when the game is started??
Morran Morran

2012/3/12

#
Something similar was asked here . The "Counter" class would have a "long" variable called "startTime". Then in your constructor for Counter, do this:
public void Counter()
{
     startTime = System.currentTimeMillis();  
}
Then in your act method:
public void act()
{
   if (startTime != 0 && startTime <= System.currentTimeMillis() + 30000)  //the 30000 part means 30 seconds
   {  
       startTime = 0;  
       doSomething();  
   }   
}
where "doSomething" is a method that does whatever you want the counter to do when it reaches the 30 second mark. For example, if you want to stop the game at that mark, do:
public void doSomething()
{
    Greenfoot.stop();
}
I hope that that helps you out.
Duta Duta

2012/3/12

#
Morran wrote...
public void act()
{
   if (startTime != 0 && startTime <= System.currentTimeMillis() + 30000)  //the 30000 part means 30 seconds
   {  
       startTime = 0;  
       doSomething();  
   }   
}
Shouldn't it be if(startTime != 0 && startTime + 30000 >= System.currentTimeMillis()) ?
Morran Morran

2012/3/12

#
You are absolutely right. I'm glad you caught that.
danpost danpost

2012/3/12

#
private boolean reached30secs = false;

public void act()
{
    if (reached30secs = false && System.currentTimeMillis() - startTime >= 30000)
    {
        doSomething();
        reached30secs = true;
    }
}
I am not sure why you all are checking to see if startTime is zero or not. It is clearly being set in the constructor to a specific time and not being changed after in the code. On the other hand, you probably want to 'doSomething()' once, and only once; therefore, I added the extra boolean check.
Duta Duta

2012/3/12

#
@danpost I thought the same but it does exactly the same job, however the boolean version means you store 2 variables instead of one
danpost danpost

2012/3/12

#
I see the madness in the method. Thanks for pointing that out.
Duta Duta

2012/3/12

#
To be fair I nearly posted exactly the same reply
joemoma joemoma

2012/3/13

#
what is the "long" variable you are talking about??
Greenfoot15 Greenfoot15

2012/3/13

#
im the same guy with the problem i was just on my friends account..it cant find variable "startTime" do i define what startTime is at the top?
Morran Morran

2012/3/13

#
Yes, you have to create the variable "startTime" somewhere in your code, preferably at the top or bottom of your class, but outside of any methods. If you don't know how to do it, it's very simple to do, just put this:
private long startTime;
either at the top of the class or the bottom of the class.
Greenfoot15 Greenfoot15

2012/3/13

#
the game still wont do the counter. when i put the counter into the game it shows the little greenfoot but does not count.
Morran Morran

2012/3/13

#
Did you initialize "startTime" in the constructor of your Counter? Maybe that's the problem... You can also right click the Counter and then click inspect to see exactly what value "startTime" has at any point in the execution.
Greenfoot15 Greenfoot15

2012/3/14

#
nope...that was problem haha.. thank you
matt.milan matt.milan

2012/3/14

#
I like this counter better than the basic "int--, if < 0, false "ones i've been making, since ( i think) it's not affected by the speed slider of greenfoot.
There are more replies on the next page.
1
2