Game frame speed processing
It is important to maintain the smoothness of the game on different performance mobile phone platforms. Therefore, we need to set appropriate frame speeds for hardware that cannot be connected. Generally
If the frame speed of a mobile phone user is 40/s, the related parameters and Code are as follows:
// ================================================ ====================
// Adjust the frame speed of the game
// ================================================ ====================
Private long framerate = 1000/40; // sets the frame speed to 40 frames per second (25 MS/frame)
Private long framestart; // The time when the frame starts. The frame begin
Private long framecount = 0; // Number of frames in this second
Private long elapsedtime; // time consumed by one frame
Private long totalelapsedtime = 0; // time consumed by multiple frames
Private long reportedframerate; // The actually calculated frame rate.
Public void updateframe ()
{
Framestart = system. currenttimemillis ();
Repaint ();
Elapsedtime = system. currenttimemillis ()-framestart;
// Frame Rate Synchronization
Try {
If (elapsedtime <framerate ){
Thread. Sleep (framerate-elapsedtime );
} Else {
Thread. Sleep (5 );
}
} Catch (exception e ){
E. printstacktrace ();
}
// Update the actual Frame Rate
++ Framecount;
Totalelapsedtime + = (system. currenttimemillis ()-framestart );
If (totalelapsedtime & gt; 1000 ){
Reportedframerate = (long) (double) framecount/(double)
Totalelapsedtime * 1000.0 );
// The frame rate can be displayed on the screen during debugging.
// G. drawstring ("FPS:" + reportedframerate, X, Y, 0 );
Framecount = 0;
Totalelapsedtime = 0;
}
}