cocos2dx 在android下的帧频设置问题

来源:互联网 发布:android sdk linux 编辑:程序博客网 时间:2024/04/30 03:03

今天又遇到一个神坑,通过setAnimationInterval设置帧频为30帧,发现在android下没有效果。

最后在Cocos2dxRenderer .java文件里面找到了这个函数。里面有句注释 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;
*/

}


---------------------------------------------------------------------------------------------------------

所以只能在程序逻辑上多做控制,用时间去控制动作和其他逻辑。


private long renderingElapsedTime;@Overridepublic 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);}
参考了下网上的解决方案,在真机上测试基本可以达到要求