My Gameloop is messing with me!!! Stuck at 30FPS

i had another gameloop before but tried this one still does the same thing
its stuck at 30Fps for some reason

my Jframe is on FullScreen might be good to know!
any ideas why?

am not rendering anything or doing anyting it just gets stuck on 30 for some reason :frowning:

Console

(FPS: 0)
(FPS: 1)
(FPS: 2)
(FPS: 3)
(FPS: 4)
(FPS: 5)
(FPS: 6)
(FPS: 7)
(FPS: 8)
(FPS: 9)
(FPS: 10)
(FPS: 11)
(FPS: 12)
(FPS: 13)
(FPS: 14)
(FPS: 15)
(FPS: 16)
(FPS: 17)
(FPS: 18)
(FPS: 19)
(FPS: 20)
(FPS: 21)
(FPS: 22)
(FPS: 23)
(FPS: 24)
(FPS: 25)
(FPS: 26)
(FPS: 27)
(FPS: 28)
(FPS: 29)
(FPS: 30)
(FPS: 0)
(FPS: 1)
(FPS: 2)
(FPS: 3)
(FPS: 4)
(FPS: 5)
(FPS: 6)
(FPS: 7)
(FPS: 8)
(FPS: 9)
(FPS: 10)
(FPS: 11)
(FPS: 12)
(FPS: 13)
(FPS: 14)
(FPS: 15)
(FPS: 16)
(FPS: 17)
(FPS: 18)
(FPS: 19)
(FPS: 20)
(FPS: 21)
(FPS: 22)
(FPS: 23)
(FPS: 24)
(FPS: 25)
(FPS: 26)
(FPS: 27)
(FPS: 28)
(FPS: 29)
(FPS: 30)
(FPS: 0)
(FPS: 1)
(FPS: 2)
(FPS: 3)
(FPS: 4)
(FPS: 5)
(FPS: 6)
(FPS: 7)
(FPS: 8)
(FPS: 9)
(FPS: 10)
(FPS: 11)
(FPS: 12)
(FPS: 13)
(FPS: 14)
(FPS: 15)
(FPS: 16)
(FPS: 17)
(FPS: 18)
(FPS: 19)
(FPS: 20)
(FPS: 21)
(FPS: 22)
(FPS: 23)
(FPS: 24)
(FPS: 25)
(FPS: 26)
(FPS: 27)
(FPS: 28)
(FPS: 29)
(FPS: 30)
@Override
	public void run() {
		
		init();
		
		long lastLoopTime = System.nanoTime();
		final int TARGET_FPS = 60;
		final long OPTIMAL_TIME = 1000000000 / TARGET_FPS; 

		
		
		while(running){
			long now = System.nanoTime();
			long updateLength = now - lastLoopTime;
			lastLoopTime = now;
			
			double deltaTime = updateLength / ((double)OPTIMAL_TIME);

			lastFpsTime += updateLength;
			frames++;
			 
			
			if (lastFpsTime >= 1000000000)
		    {
		         System.out.println("(FPS: "+frames+")");
		         lastFpsTime = 0;
		         frames = 0;
		    }
			
			System.out.println("(FPS: "+frames+")");
			
		    if(!isPaused()){
				tick(deltaTime);
				clear();
				render();
		    }else{
		    	Pausedtick(deltaTime);
		    	Pausedrender();
		    }

		   try{
			   Thread.sleep((lastLoopTime-System.nanoTime() * OPTIMAL_TIME) / 1000000);
		   }catch(Exception e){
			   
		   }
		}
	}

The Clear Method

	public void clear() {
		Graphics g2 = getGraphics();
		if(img != null){
			g2.drawImage(img, 0, 0, null);
		}
		g2.dispose();
	}

The Render Method

	
	public void render(){
		graphics2d.clearRect(0, 0, width, height);
		graphics2d.drawString(getFps()+"", 20, 20);
	}

Where did you get this code?

here: http://www.java-gaming.org/index.php?topic=24220.0

… now am suddently running fps on

(FPS: 57254)
(FPS: 57255)
(FPS: 57256)
(FPS: 57257)
(FPS: 57258)
(FPS: 57259)
(FPS: 57260)
(FPS: 57261)
(FPS: 57262)
(FPS: 57263)
(FPS: 57264)
(FPS: 57265)
(FPS: 57266)
(FPS: 57267)
(FPS: 57268)
(FPS: 57269)
(FPS: 57270)
(FPS: 57271)
(FPS: 57272)
(FPS: 57273)
(FPS: 57274)
(FPS: 57275)
(FPS: 57276)
(FPS: 57277)
(FPS: 57278)
(FPS: 57279)
(FPS: 57280)
(FPS: 57281)
(FPS: 57282)
(FPS: 57283)
(FPS: 57284)
(FPS: 57285)
(FPS: 57286)
(FPS: 57287)
(FPS: 57288)
(FPS: 57289)
(FPS: 57290)
(FPS: 57291)
(FPS: 57292)
(FPS: 57293)
(FPS: 57294)
(FPS: 57295)
(FPS: 57296)
(FPS: 57297)
(FPS: 57298)
(FPS: 57299)
(FPS: 57300)
(FPS: 57301)
(FPS: 57302)
(FPS: 57303)
(FPS: 57304)
(FPS: 57305)
(FPS: 57306)
(FPS: 57307)
(FPS: 57308)
(FPS: 57309)
(FPS: 57310)
(FPS: 57311)
(FPS: 57312)
(FPS: 57313)
(FPS: 57314)
(FPS: 57315)
(FPS: 57316)
(FPS: 57317)
(FPS: 57318)
(FPS: 57319)
(FPS: 57320)
(FPS: 57321)
(FPS: 57322)
(FPS: 57323)
(FPS: 57324)
(FPS: 57325)
(FPS: 57326)
(FPS: 57327)
(FPS: 57328)
(FPS: 57329)
(FPS: 57330)
(FPS: 57331)
(FPS: 57332)
(FPS: 57333)
(FPS: 57334)
(FPS: 57335)
(FPS: 57336)
(FPS: 57337)
(FPS: 57338)
(FPS: 57339)
(FPS: 57340)
(FPS: 57341)
(FPS: 57342)
(FPS: 57343)
(FPS: 57344)
(FPS: 57345)
(FPS: 57346)
(FPS: 57347)
(FPS: 57348)
(FPS: 57349)
(FPS: 57350)
(FPS: 57351)
(FPS: 57352)
(FPS: 57353)
(FPS: 57354)
(FPS: 57355)
(FPS: 57356)
(FPS: 57357)
(FPS: 57358)
(FPS: 57359)
(FPS: 57360)
(FPS: 57361)
(FPS: 57362)
(FPS: 57363)
(FPS: 57364)
(FPS: 57365)
(FPS: 57366)
(FPS: 57367)
(FPS: 57368)
(FPS: 57369)
(FPS: 57370)
(FPS: 57371)
(FPS: 57372)
(FPS: 57373)
(FPS: 57374)
(FPS: 57375)
(FPS: 57376)
(FPS: 57377)
(FPS: 57378)
(FPS: 57379)
(FPS: 57380)
(FPS: 57381)
(FPS: 57382)
(FPS: 57383)
(FPS: 57384)
(FPS: 57385)
(FPS: 57386)
(FPS: 57387)
(FPS: 57388)
(FPS: 57389)
(FPS: 57390)
(FPS: 57391)
(FPS: 57392)
(FPS: 57393)
(FPS: 57394)
(FPS: 57395)
(FPS: 57396)
(FPS: 57397)
(FPS: 57398)
(FPS: 57399)
(FPS: 57400)
(FPS: 57401)
(FPS: 57402)
(FPS: 57403)
(FPS: 57404)
(FPS: 57405)
(FPS: 57406)
(FPS: 57407)
(FPS: 57408)
(FPS: 57409)
(FPS: 57410)
(FPS: 57411)
(FPS: 57412)
(FPS: 57413)
(FPS: 57414)
(FPS: 57415)
(FPS: 57416)
(FPS: 57417)
(FPS: 57418)
(FPS: 57419)
(FPS: 57420)
(FPS: 57421)
(FPS: 57422)
(FPS: 57423)
(FPS: 57424)
(FPS: 57425)
(FPS: 57426)
(FPS: 57427)
(FPS: 57428)
(FPS: 57429)
(FPS: 57430)
(FPS: 57431)
(FPS: 57432)
(FPS: 57433)
(FPS: 57434)
(FPS: 57435)
(FPS: 57436)
(FPS: 57437)
(FPS: 57438)
(FPS: 57439)
(FPS: 57440)
(FPS: 57441)
(FPS: 57442)
(FPS: 57443)
(FPS: 57444)
(FPS: 57445)
(FPS: 57446)
(FPS: 57447)
(FPS: 57448)
(FPS: 57449)
(FPS: 57450)
(FPS: 57451)
(FPS: 57452)
(FPS: 57453)
(FPS: 57454)
(FPS: 57455)
(FPS: 57456)
(FPS: 57457)
(FPS: 57458)
(FPS: 57459)
(FPS: 57460)
(FPS: 57461)
(FPS: 57462)
(FPS: 57463)
(FPS: 57464)
(FPS: 57465)
(FPS: 57466)

did you change anything?

From

		    if(!isPaused()){
				tick(deltaTime);
				clear();
				render();
		    }else{
		    	Pausedtick(deltaTime);
		    	Pausedrender();
		    }

to

//		    if(!isPaused()){
				tick(deltaTime);
				render();
				
				clear();
//		    }else{
//		    	Pausedtick(deltaTime);
//		    	Pausedrender();
//		    }

but it still does not go to 60 when i render stuff when i had over 70000 fps i removed that code completly

am i suppose to do this?

	public void clear() {
		Graphics g2 = getGraphics();
		if(img != null){
			g2.drawImage(img, 0, 0, null);
		}
		g2.dispose();
	}

??

What i use


long lastTime = System.nanoTime();
		// 60D is how many ticks per seccond
		double nsPerTick = 1000000000D / 60D;
		int frames = 0;
		int ticks = 0;
		long lastTimer = System.currentTimeMillis();
		double delta = 0;
		while (isGameRunning) {
			long now = System.nanoTime();
			delta += (now - lastTime) / nsPerTick;
			lastTime = now;
			boolean shouldRender = false;

			while (delta >= 1) {
				ticks++;
				tick();
				delta -= 1;
				shouldRender = true;
			}
			if (shouldRender == true) {
				frames++;
				render();
			}
			if (System.currentTimeMillis() - lastTimer >= 1000) {
				lastTimer += 1000;
				System.out.println("FPS:" + frames + ",TPS:" + ticks);
				tps = frames;
				fps = ticks;
				frames = 0;
				ticks = 0;
			}
		}

Like others have said in pretty much every thread you’ve posted, you need to take a step back and start with code you can understand.

Start smaller. Can you first get a basic thread running? Post that.

Then can you add logic that makes it sleep for a certain amount of time? Post that.

When you have that working (and only when you have that working), can you add logic that sleeps for a certain amount of time, based on how much time has elapsed during a given frame? Post that.

Copy-pasting code and hoping for the best ain’t gonna work.

Another suggestion: Use a library which handles the game loop for you. Doing it yourself with Java2D is an unnecessary amount of work and won’t benefit you in the slightest.

  • Jev

SomeTimes the player laggs forward coos of deltaTime it goes from 0 up to 5 any fix for that besides fixed TimeStep?

[icode]sleep(a-b * time)[/icode] != [icode]sleep((a-b) * time)[/icode]

Thats why my code is heavily bracketed I know that @burntpizza will understand it hes a fan of lisp but it makes me look a bit like a C coder with all the spaghetti.