Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/199.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android Cocos2d-x setAnimationInterval不';我不能在安卓上工作_Android_C++_Eclipse_Cocos2d X_Frame Rate - Fatal编程技术网

Android Cocos2d-x setAnimationInterval不';我不能在安卓上工作

Android Cocos2d-x setAnimationInterval不';我不能在安卓上工作,android,c++,eclipse,cocos2d-x,frame-rate,Android,C++,Eclipse,Cocos2d X,Frame Rate,我尝试使用以下代码在Cocos2d-x中的应用程序中设置最大FPS: CCDirector::sharedDirector()->setAnimationInterval(1.0 / 30); 它在iOS上工作,但当我在三台Android设备上测试它时,它被忽略,并以标准间隔(1/60)渲染帧 如何使用cocos2d-x在Android上正确设置最大FPS?这是一件很好的事情。但是如果两个场景同时播放,并且两个场景都是30 FPS,虽然pDirector->getAnimationInt

我尝试使用以下代码在Cocos2d-x中的应用程序中设置最大FPS:

CCDirector::sharedDirector()->setAnimationInterval(1.0 / 30);
它在iOS上工作,但当我在三台Android设备上测试它时,它被忽略,并以标准间隔(1/60)渲染帧


如何使用cocos2d-x在Android上正确设置最大FPS?

这是一件很好的事情。但是如果两个场景同时播放,并且两个场景都是30 FPS,虽然
pDirector->getAnimationInterval()
返回1/30的间隔,但仍然可以获得60+FPS。所以我实际上已经设法实现了这一点。您必须编辑Cocos2dxRenderer.java文件,然后清理并重建Cocos2d-x

代码如下:

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 (loopRuntime < 40) {
            Log.wtf("RENDERER", "Sleeping for == " + (40 - loopRuntime));
            Thread.sleep(40 - loopRuntime);
        }
    } catch (InterruptedException e) {
        e.printStackTrace();
    }

    //final long nowInNanoSeconds = System.nanoTime();
    //final long interval = nowInNanoSeconds - this.mLastTickInNanoSeconds;

            loopStart = System.currentTimeMillis();

    // should render a frame when onDrawFrame() is called or there is a
    // "ghost"
    Cocos2dxRenderer.nativeRender();

    loopEnd = System.currentTimeMillis();
    loopRuntime = (loopEnd - loopStart);

    Log.wtf("RENDERER", "loopRunTime == " + loopRuntime);

    // 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;
}
public void onDrawFrame(最终GL10 gl){
//FPS控制算法不准确,会降低FPS速度
//在某些设备上。因此请注释FPS控制代码。
试一试{
如果(循环运行时间<40){
wtf(“渲染器”,“睡眠时间=”+(40-loopRuntime));
sleep(40-loopRuntime);
}
}捕捉(中断异常e){
e、 printStackTrace();
}
//final long nowInNanoSeconds=System.nanoTime();
//最终长间隔=nowinnanoses-this.mlasttickinnanoses;
loopStart=System.currentTimeMillis();
//应在调用onDrawFrame()或存在
//“鬼魂”
Cocos2dxRenderer.nativeRender();
loopEnd=System.currentTimeMillis();
loopRuntime=(loopEnd-loopStart);
wtf(“渲染器”,“循环运行时=”+循环运行时);
//fps控制
/*if(间隔
奇怪的是,当我取消对fps控件的注释时,它什么也没做,而当我编写我的版本时,它却做了。。。 无论如何,40的“magic”值大约为35fps,但您当然可以轻松地将其更改为使用通过setAnimationInterval()传递的值


编辑:我将loopStart行移动到睡眠后->睡眠时间不应包含在loopTime中。

此代码工作正常

发现于

private long renderingElapsedTime;
@凌驾
公共框架(最终GL10 gl)
{
/*
*FPS控制算法不准确,会降低FPS速度
*在某些设备上。因此请注释FPS控制代码。
*/
试一试{
if(renderingElapsedTime*纳秒PermitCrosecond
}

希望能有帮助

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 * NANOSECONDSPERMICROSECOND < Cocos2dxRenderer.sAnimationInterval) {
        Thread.sleep((Cocos2dxRenderer.sAnimationInterval - renderingElapsedTime * NANOSECONDSPERMICROSECOND) / NANOSECONDSPERMICROSECOND);
    }
} catch (InterruptedException e) {
    e.printStackTrace();
}

/*
final long nowInNanoSeconds = System.nanoTime();
final long interval = nowInNanoSeconds - this.mLastTickInNanoSeconds;
*/

// Get the timestamp when rendering started
long renderingStartedTimestamp = System.currentTimeMillis();

// should render a frame when onDrawFrame() is called or there is a
// "ghost"
Cocos2dxRenderer.nativeRender();

// Calculate the elapsed time during rendering
renderingElapsedTime = (System.currentTimeMillis() - renderingStartedTimestamp);

/*
// 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;
*/