环境:
cocos creator 2.4.3
华为meta 50
背景:
小游戏 需要在update 里取帧率 发现跟时间对不上
游戏设置60帧
手机上显示 90帧
cc.game.setFrameRate(30) 显示 30帧
cc.game.setFrameRate(60) 显示 90帧
结论:
对于老版本的cocos creator 如果有依赖帧率的逻辑
需要自行注意逻辑正确性
cc.game.getFrameRate() 取出来的帧率 有可能 并不是当前真实帧率 只是逻辑帧率
源码分析:
java
cocos2dxRenderer.java
@Override
public void onDrawFrame(final GL10 gl) {
if (mNeedToPause)
return;
if (mNeedShowFPS) {
/
//IDEA: show FPS in Android Text control rather than outputing log.
++mFrameCount;
long nowFpsTime = System.nanoTime();
long fpsTimeInterval = nowFpsTime - mOldNanoTime;
if (fpsTimeInterval > 1000000000L) {
double frameRate = 1000000000.0 * mFrameCount / fpsTimeInterval;
Cocos2dxHelper.OnGameInfoUpdatedListener listener = Cocos2dxHelper.getOnGameInfoUpdatedListener();
if (listener != null) {
listener.onFPSUpdated((float) frameRate);
}
mFrameCount = 0;
mOldNanoTime = System.nanoTime();
}
/
}
/*
* No need to use algorithm in default(60 FPS) situation,
* since onDrawFrame() was called by system 60 times per second by default.
*/
if (sAnimationInterval <= INTERVAL_60_FPS) {
Cocos2dxRenderer.nativeRender();
} else {
final long now = System.nanoTime();
final long interval = now - this.mLastTickInNanoSeconds;
if (interval < Cocos2dxRenderer.sAnimationInterval) {
try {
Thread.sleep((Cocos2dxRenderer.sAnimationInterval - interval) / Cocos2dxRenderer.NANOSECONDSPERMICROSECOND);
} catch (final Exception e) {
}
}
/*
* Render time MUST be counted in, or the FPS will slower than appointed.
*/
this.mLastTickInNanoSeconds = System.nanoTime();
Cocos2dxRenderer.nativeRender();
}
}
分析上述代码
java
if (sAnimationInterval <= INTERVAL_60_FPS) {
Cocos2dxRenderer.nativeRender();
}
说明 >= 60帧的设置 。没有处理刷新帧率 走的系统是高刷
所以会导致 dt 并不是 60帧的dt