Results 1 to 2 of 2
  1. #1
    Zelaine is offline Senior Member
    Join Date
    Aug 2013
    Location
    Sweden
    Posts
    163
    Rep Power
    6

    Default Thread Sleep Lag

    Hi! I'm creating a game, and have tried to create a game loop with information I've accumulated online. But the thing is, with my loop the game lags and I don't really understand why. Could someone enlighten me, please?

    Java Code:
    ...
    
    public class Main extends JFrame{
    	...
    
    	public Main(){
    		...
    
    		canvas = new Drawing();
    		add(canvas);
    
    		gameLoop();
    	}
    	
    	...
    
    	public void gameLoop(){
    		long cTime = System.nanoTime();
    		long lSecond = 0;
    		int fps = 0;
    		running = true;
    
    		while(running){
    			long now = System.nanoTime();
    			long dTime = (now - cTime);
    			cTime = now;
    			double delta = dTime/((double)TICKS);
    
    			lSecond += dTime;
    			fps++;
    
    			if(lSecond >= 1000000000){
    				//System.out.println("FPS: " + fps);
    				lSecond = 0;
    				fps = 0;
    			}
    			
    			gameUpdate(delta);
    
    			canvas.repaint();
    			
    			try{
    				Thread.sleep((cTime - System.nanoTime() + TICKS) / 1000000);
    			}catch(InterruptedException e){
    				e.printStackTrace();
    			}
    		}
    	}
    
    	public void gameUpdate(double delta){
    		...
    	}
    	
    	...
    
    	private class Drawing extends JPanel{
    		public Drawing(){
    			setPreferredSize(new Dimension(WIDTH, HEIGHT));
    		}
    
    		@Override
    		public void paintComponent(Graphics g){
    			super.paintComponent(g);
    			
    			...
            }
    	}
    
    	public static void main(String args[]){
    		new Main();
    	}
    }
    Thanks in advance!

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Thread Sleep Lag

    Not much information to go on. I recommend you submit an Short, Self Contained, Correct Example that demonstrates the problem.
    Keep it short and compilable. And it would help if you documented your code as to what you are trying to do.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Replies: 1
    Last Post: 09-24-2012, 07:01 PM
  2. Difference between Thread.yield() and Thread.sleep() methods
    By Nageswara Rao Mothukuri in forum New To Java
    Replies: 12
    Last Post: 07-30-2010, 05:37 PM
  3. how to reduce the thread sleep time and wake up the thread
    By baktha.thalapathy in forum Threads and Synchronization
    Replies: 2
    Last Post: 06-24-2010, 07:36 PM
  4. thread.sleep
    By jc5 in forum CLDC and MIDP
    Replies: 0
    Last Post: 09-02-2009, 11:51 PM
  5. How to use the sleep and thread?
    By jiuhu in forum Java Applets
    Replies: 4
    Last Post: 08-07-2007, 02:56 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •