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

2012/1/10

new to java programming how do i add a time limit to my card memory game..

Waynesworld85 Waynesworld85

2012/1/10

#
Help me please it is a project for my VGP class and it due tomoorow at midnight.. any help would be greatly appreciated. i cant figure out how to add a time limit for my memory card game..
danpost danpost

2012/1/11

#
I posted a sample scenario called Millisecond Timer with the source code provided. It is for anybody to make use of. You would have to copy/paste or create similar code and adjust it for your needs. For a time limit, you would want to check currentTime - startTime continuously until a certain period of time has elapsed.
if (System.currentTimeMillis() - startTime > timeLimit) { }
Just add code in curly brackets for what to do when time has expired (and replace 'timeLimit' with a valid value in milliseconds).
Duta Duta

2012/1/12

#
Alternatively, you just do the following
//Declare this field:
private int timer = 0;
//Include this in your act() method:
public void act()
{
    //Your code
    timer++;
}
//And then have this somewhere:
if(timer > 1000) //Replace 1000 with your limit
{
    //Do whatever
}
//To reset the timer, just do:
timer = 0;
One thing to bear in mind is that timer is increased by one every act() method, so its not in "real" time, like seconds/minutes
extremejava extremejava

2012/1/13

#
You can either use the java.util.Date class or as suggested above use the System.currentTimeMillis() approach. Both are effectively same. Java Classpath in Windows
You need to login to post a reply.