Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Java timer while playing the game
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
hamid14




PostPosted: Mon Sep 26, 2011 6:44 pm   Post subject: Java timer while playing the game

I have a game which was made using java, its an applet. I'm trying to create a 60-second timer that gives you 60 seconds to do certain tasks in the game before it hits 0. I created a timer like this with the actionPerformed method;

private Timer timer;

timer = new Timer(1000,this);

public void actionPerformed(ActionEvent e) {
repaint();
}

then i just have the actual time variable like this;

time--;

but the problem is, it still goes too fast, its not going down by 1 every second.

How would I make it decrease every second by 1 using timer? thanks for any help.
Sponsor
Sponsor
Sponsor
sponsor
Zren




PostPosted: Mon Sep 26, 2011 7:35 pm   Post subject: Re: Java timer while playing the game

B will overtake A as A is doing extra calculations before waiting another second.
Basically you'd have another timertask that you'd schedule for when your timer hit's zero. Also, don't forget to purge the timer when you complete the tasks game before it's the timer is done.

/me isn't feeling like giving the theory/hints first.

Java:

import java.util.Timer;
import java.util.TimerTask;


public class Test {
        public static void main(String[] args) {
                // ---- With Timers ----
                Timer timer = new Timer();
               
                // A
                // Modify a referenced variable every second.
                Integer count = 0; // A reference to an integer
                timer.schedule(new SayMsgTickerTask("A", count), 0, 1000);
               
                // B
                // Load all scheduled tasks at the beginning.
                int start = 0;
                for (int i = 0; i <= 10; i++) {
                        int t = start + i * 1000;
                        timer.schedule(new SayMsgTask("B", i), t);
                }
    }
}

class SayMsgTask extends TimerTask {
        private String msg;
        private int count;
       
        public SayMsgTask(String msg, int count) {
                this.msg = msg;
                this.count = count;
        }
       
        @Override
        public void run() {
                System.out.println(String.format("%s: %d", msg, count));
        }
}

class SayMsgTickerTask extends TimerTask {
        private String msg;
        private Integer count;
       
        public SayMsgTickerTask(String msg, Integer count) {
                this.msg = msg;
                this.count = count;
        }
       
        @Override
        public void run() {
                System.out.println(String.format("%s: %d", msg, count));
               
                count += 1;
               
                // When referenced variable provides exit condition, take it.
                if (count > 10)
                        cancel();
        }
}
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 2 Posts ]
Jump to:   


Style:  
Search: