我的游戏在大多数手机上表现良好(56帧每秒),但其他手机只有大约25帧每秒。我在游戏中使用了3个粒子系统,据我所知问题就出在这里。我的问题是:如果检测到帧率低于30,停止产生粒子是否是一个好主意?如果帧率高则正常运行。我不确定这是否会引起任何问题。还有其他的解决方案吗?
我的游戏在大多数手机上表现良好(56帧每秒),但其他手机只有大约25帧每秒。我在游戏中使用了3个粒子系统,据我所知问题就出在这里。我的问题是:如果检测到帧率低于30,停止产生粒子是否是一个好主意?如果帧率高则正常运行。我不确定这是否会引起任何问题。还有其他的解决方案吗?
我可以想到几件事情来帮助缓解这个问题。
你可以使用你的方法来检测fps,然后在必要时丢弃粒子系统。但是,你不需要每秒轮询一次 - 你可以每十秒钟左右轮询一次。如果你确实有一个低帧率,那么你就知道手机偶尔会受到影响,那么你就不需要再轮询了 - 你可以降低粒子数量并停止轮询。
或者,你可以使用轮询方法和几个“级别”的粒子效果,所以如果无法处理顶层,就放弃下一个复杂度级别,依此类推。
此外,你甚至可以允许用户手动调整粒子效果,在选项菜单中允许他们将其切换到低设置或其他设置。
也许你还可以在运行时对设备进行配置文件分析,例如核心数、图形芯片等,并作出相应调整。
确保你的粒子已经优化过了,例如当它们可以更小时,不要使用不必要的纹理尺寸,但我相信你可能已经做到了!
<?xml version="1.0" encoding="UTF-8"?>
<resources>
<style name="Theme.NoBackground" parent="android:Theme">
<item name="android:windowBackground">@null</item>
</style>
</resources>
如果帧率变慢,请尝试跳过一些帧,但不要跳过太多,否则会导致用户体验差。以下是用于确定线程是否按时执行的代码,如果没有按时执行,它将跳过帧(不超过5帧):
// desired fps
private final static int MAX_FPS = 50;
// maximum number of frames to be skipped
private final static int MAX_FRAME_SKIPS = 5;
// the frame period
private final static int FRAME_PERIOD = 1000 / MAX_FPS;
// number of frames skipped since the game started
private long totalFramesSkipped = 0l;
// number of frames skipped in a store cycle (1 sec)
private long framesSkippedPerStatCycle = 0l;
// number of rendered frames in an interval
private int frameCountPerStatCycle = 0;
private long totalFrameCount = 0l;
// the last FPS values
private double fpsStore[];
// the number of times the stat has been read
private long statsCount = 0;
// the average FPS since the game started
private double averageFps = 0.0;
long beginTime; // the time when the cycle begun
long timeDiff; // the time it took for the cycle to execute
int sleepTime; // ms to sleep (<0 if we're behind)
int framesSkipped; // number of frames being skipped
sleepTime = 0;
beginTime = System.currentTimeMillis();
framesSkipped = 0; // resetting the frames skipped
// calculate how long did the cycle take
timeDiff = System.currentTimeMillis() - beginTime;
// calculate sleep time
sleepTime = (int)(FRAME_PERIOD - timeDiff);
if (sleepTime > 0) {
// if sleepTime > 0 we're OK
try {
// send the thread to sleep for a short period
// very useful for battery saving
Thread.sleep(sleepTime);
} catch (InterruptedException e) {}
}
while (sleepTime < 0 && framesSkipped < MAX_FRAME_SKIPS) {
// we need to catch up
this.gamePanel.update(); // update without rendering
sleepTime += FRAME_PERIOD; // add frame period to check if in next frame
framesSkipped++;
}
请告诉我这是否有效。