My Android Game Loop problems

Hey all, I’m using the loop below and it works great until I introduce a notifier to cap my rendering to 30 FPS (Basically so that it doesn’t render redundant frames and only renders when an update has occured)

Initially I was having problems with the sprites being choppy so added interpolation and it really helped to smooth things out, but as I say, on capping the rendering it has re-introduces intermittent (although not terrible) choppiness.

Any ideas? Thanks! :clue:



 @Override
 		 	 public void run() {
 		 		 
 		 		 nextGameTick = System.nanoTime();				
 	 		 	 Canvas c;
 		 	     while (running) {
 		 	     	 c=null;
 		 	         loops = 0;
 		 	   if (gameTimer==0){gameTimer=System.nanoTime();//frame=0;
 		 	  }  
 		 	    while(System.nanoTime() > nextGameTick && loops < maxFrameskip){
 		 	    	
 		 	    	updateMenu();				
 		 	    	nextGameTick += skipTicks;	
 		 	    	loops++;				
 		 	    
 		 	    } 		 	
 		 	    
 		  		  float interpolation = (float)(System.nanoTime() + skipTicks - nextGameTick)
	              / (float)(skipTicks);
 		  		  
  		  	  if (updateGame == true) {							//Has logic routine run? If so, then render
 		  		 
 		  		    
 		 	    try{c=mySurfaceHolder.lockCanvas(null);									
 				
				synchronized(mySurfaceHolder){
				
					   
	 		 	
	 		 	  
					draw(c, interpolation);								
					updateGame=false;
				}
								
			}
			finally{
					
				if (c != null){
				mySurfaceHolder.unlockCanvasAndPost(c);						
			
				                             }
		                                   	}
				               	} 
 		  	  
 		  	              	 }
	                        }
 		 	 }
 		 	 
 		 	
 		 	 
 		 	}
 	


I’m setting updateGame to true in my updateMenu() method.

PS - After changing Systemclock.elapsedRealtime() to System.Nanotime() it seems to have helped a little, but it’s still not perfect.

I’m not creating any objects at run time and GC isn’t being triggered but this is probably not relevant anyway as if I don’t cap the rendering, it seems to run a lot smoother (which is kind of odd, as it’s only then rendering the same frame as before i.e., no sprite movements).

My guess is that my capping/notification is somehow playing havoc with interpolation calculation? (timing issue?)

Thanks again!