Thursday 15 September 2011

eclipse - Simple java game ,problems with creating a timer -


I am making a simple game where you control a ball and you can Avoid

The idea is that there will be a timer, which will start from 0 and after about 10 seconds, it is going to move along with the moving obstacles to the second level

I have some problems in creating a timer, which will start counting, I tried to:

  Abstract Vjnik static void Timer () {int count = 0; While (count & lt; 10) {try {thread.sleep (1000); } Hold (exception e) {} calculation ++; } System.out.println (count);}}  

The timer starts, but for some reason, the program is not like the "Thread Sleep" part, and all the graphics discovers JFrame . The game is still running behind, maybe there might be a better way to do this?

Update: My class is down for the game without my timer: P

  Expands the public class game; JPNL Action Lustner (Timer Main T; User User; Int ostensible = 5; Int levelnumber = 1; Stable Array list & quot; Obstacles & quot; barriers = new arrelist & lt; barriers & gt; (); random rand = new random (); public game () {setFocusable (true ); User = new user (270,250); AddKeyListener (new key (users) Gator); MainT = new timer (10, this); mainT.start (); for game start (); (int i = 0; i & lt; barriers; i ++) {addObstacles (new barriers NXTIN (600), RAND.NXITIN (600));}} Public Zero Color (Graphics G) {// Paint Method Superpant (G); Graphics 2DG2D = (Graphics 2D) G; user For obstacles for draw (G2D); // (int i = 0; i & lt; barriers.) (I ++) {barriers interrupted = obstructions (I); obst.draw (G2D);}} Public Zero Methodology (ActionEvent arg0) {// Action Listener user.update (); (Brick e = 0; E & lt; Obstacles. Size (); I ++) {obstructions obstructed = obstacles .get (i); Obst.update (); } Color (); } Public Zero addObstacles (Obstacles ob) {obstacles.add (ob); } Public Fixed Arrestist & lt; Obstacles & gt; GetObstacles () {Return Obstacles; } Public stable zero gameover () {System.out.println ("Game over!"); System.exit (0); } Public Zero startgame () {barriers = levelnumber * 1; (Int i = 0; i & lt; barriers; i ++) {addObstacles (new barriers (RAND.NETXTIN (100), RAND.NXITIN (600)); }}}}  

Yes, you can add a flag variable to your timer function . If you're just trying to delay the action of something, then try to do something like this in your action for the timer

  if (counter! = 0) {counter ++; If (counter == 10) {// count to 10ms counter = 1; // whatever you want to do after the delay}} and {counter = 0; }  

No comments:

Post a Comment