cocos2dx 在android下的帧频设置问题

在尝试使用cocos2dx时,遇到通过setAnimationInterval设置为30帧但在Android设备上无效的问题。源码中发现FPS控制算法在某些设备上不准确并可能导致FPS降低。解决方案是通过程序逻辑,利用时间来控制游戏动作和逻辑。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

今天又遇到一个神坑,通过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;

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值