通过setAnimationInterval设置帧频时,发现在android下没有效果的
在Cocos2dxRenderer .java文件里面找到了onDrawFrame这个函数。里面有句注释 :
FPS controlling algorithm is not accurate, and it will slow down FPS on some devices. So comment FPS controlling code.
这个函数里的大部分代码都被注释掉了
public void onDrawFrame(final GL10 gl) { /*
* FPS controlling algorithm is not accurate, and it will slow down FPS
* on some devices. So comment FPS controlling code.
*/ /*
final long nowInNanoSeconds = System.nanoTime();
final long interval = nowInNanoSeconds - this.mLastTickInNanoSeconds; */ // should render a frame when onDrawFrame() is called or there is a
// "ghost"
Cocos2dxRenderer.nativeRender(); /*
// fps controlling if (interval < Cocos2dxRenderer.sAnimationInterval) { try {
// because we render it before, so we should sleep twice time interval
Thread.sleep((Cocos2dxRenderer.sAnimationInterval - interval) /Cocos2dxRenderer.NANOSECONDSPERMICROSECOND);
} catch (final Exception e) { }
} this.mLastTickInNanoSeconds = nowInNanoSeconds;
*/ }
经过google后暂时找到了解决方案,是否有问题需要待测试:
private long renderingElapsedTime; @Override
public void onDrawFrame(final GL10 gl) {
/*
* FPS controlling algorithm is not accurate, and it will slow down FPS
* on some devices. So comment FPS controlling code.
*/ try {
if (renderingElapsedTime< Cocos2dxRenderer.sAnimationInterval) {
Thread.sleep((Cocos2dxRenderer.sAnimationInterval - renderingElapsedTime) / NANOSECONDSPERMICROSECOND);
}
} catch (InterruptedException e) {
e.printStackTrace();
} // Get the timestamp when rendering started
long renderingStartedTimestamp = System.nanoTime(); // should render a frame when onDrawFrame() is called or there is a
// "ghost"
Cocos2dxRenderer.nativeRender(); // Calculate the elapsed time during rendering
renderingElapsedTime = (System.nanoTime() - renderingStartedTimestamp);
}