- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我的游戏基于月球着陆器演示,尽管经过大量修改,我可以获得大约 40-50fps,但问题是它在 40-50fps 之间波动太大,导致移动图形抖动!它非常烦人,让我的游戏看起来很糟糕,而实际上它以良好的帧速率运行。
我尝试将线程优先级设置得更高,但这只会让情况变得更糟......现在它会在 40-60fps 之间波动......
我正在考虑将 FPS 限制在 30 左右,以使其保持不变。这是个好主意吗?还有其他人有经验或不同的解决方案吗?
谢谢!
这是我的运行循环
@Override
public void run() {
while (mRun) {
Canvas c = null;
try {
c = mSurfaceHolder.lockCanvas(null);
synchronized (mSurfaceHolder) {
if(mMode == STATE_RUNNING){
updatePhysics();
}
doDraw(c);
}
} finally {
// do this in a finally so that if an exception is thrown
// during the above, we don't leave the Surface in an
// inconsistent state
if (c != null) {
mSurfaceHolder.unlockCanvasAndPost(c);
}
}
}
}
private void updatePhysics() {
now = android.os.SystemClock.uptimeMillis();
elapsed = (now - mLastTime) / 1000.0;
posistionY += elapsed * speed;
mLastTime = now;
}
最佳答案
不要将游戏的逻辑(对象移动等)更新速率建立在帧速率上。换句话说,将您的绘图和逻辑更新代码放在两个单独的组件/线程中。这样您的游戏逻辑就完全独立于您的帧率。
逻辑更新应该基于自上次更新以来经过了多少时间(我们称之为delta
)。因此,如果您有一个以 1 像素/毫秒的速度移动的对象,那么在每次更新期间您的对象应该执行如下操作:
public void update(int delta) {
this.x += this.speed * delta;
}
所以现在即使你的 FPS 滞后,它也不会影响你的物体的移动速度,因为 delta 会更大,使物体移动得更远以补偿(在某些情况下会很复杂,但这就是它的要点).
这是在逻辑更新对象(在某个线程循环中运行)中计算增量的一种方法:
private long lastUpdateTime;
private long currentTime;
public void update() {
currentTime = System.currentTimeMillis();
int delta = (int) (currentTime - lastUpdateTime);
lastUpdateTime = currentTime;
myGameObject.update(delta); // This would call something like the update method above.
}
希望对您有所帮助!如有其他问题请追问;我一直在自己制作 Android 游戏。 :)
示例代码:
复制这两个 fragment (1 个 Activity 和 1 个 View )并运行代码。无论您的 FPS 是多少,结果都应该是一个白点顺畅地落下您的屏幕。代码看起来有点复杂和冗长,但实际上很简单;评论应该解释一切。
这个 Activity 课不是太重要。您可以忽略其中的大部分代码。
public class TestActivity extends Activity {
private TestView view;
public void onCreate(Bundle savedInstanceState) {
// These lines just add the view we're using.
super.onCreate(savedInstanceState);
setContentView(R.layout.randomimage);
RelativeLayout rl = (RelativeLayout) findViewById(R.id.relative_layout);
view = new TestView(this);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
10000, 10000);
rl.addView(view, params);
// This starts our view's logic thread
view.startMyLogicThread();
}
public void onPause() {
super.onPause();
// When our activity pauses, we want our view to stop updating its logic.
// This prevents your application from running in the background, which eats up the battery.
view.setActive(false);
}
}
这个类是令人兴奋的地方!
public class TestView extends View {
// Of course, this stuff should be in its own object, but just for this example..
private float position; // Where our dot is
private float velocity; // How fast the dot's moving
private Paint p; // Used during onDraw()
private boolean active; // If our logic is still active
public TestView(Context context) {
super(context);
// Set some initial arbitrary values
position = 10f;
velocity = .05f;
p = new Paint();
p.setColor(Color.WHITE);
active = true;
}
// We draw everything here. This is by default in its own thread (the UI thread).
// Let's just call this thread THREAD_A.
public void onDraw(Canvas c) {
c.drawCircle(150, position, 1, p);
}
// This just updates our position based on a delta that's given.
public void update(int delta) {
position += delta * velocity;
postInvalidate(); // Tells our view to redraw itself, since our position changed.
}
// The important part!
// This starts another thread (let's call this THREAD_B). THREAD_B will run completely
// independent from THREAD_A (above); therefore, FPS changes will not affect how
// our velocity increases our position.
public void startMyLogicThread() {
new Thread() {
public void run() {
// Store the current time values.
long time1 = System.currentTimeMillis();
long time2;
// Once active is false, this loop (and thread) terminates.
while (active) {
try {
// This is your target delta. 25ms = 40fps
Thread.sleep(25);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
time2 = System.currentTimeMillis(); // Get current time
int delta = (int) (time2 - time1); // Calculate how long it's been since last update
update(delta); // Call update with our delta
time1 = time2; // Update our time variables.
}
}
}.start(); // Start THREAD_B
}
// Method that's called by the activity
public void setActive(boolean active) {
this.active = active;
}
}
关于Android 2d Canvas 游戏: FPS Jitter problem,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3312720/
我有一个导航栏,当您将鼠标悬停在导航按钮上时,我想给它一个橙色的底部边框。唯一的问题是,无论何时悬停,边框都会使内容/导航按钮“抖动”,这是它们不应该的。此外,我已经在导航栏上始终设置了黑色底部边框,
根据documentation ,jitter 函数的解释是“向数值向量添加少量噪声。” 这是什么意思? 是否有一个随机数与向量中的每个数字相关联并添加到其中? 最佳答案 抖动确实意味着只是将随机噪声
假设我有这样的东西: for (int i = 0; i > 1或 x << 1 . Does the jitter actually do so when it is legal? 我不知道。试试吧
我正在使用 Photos 框架和 requestImageForAsset: 花费太多时间,因此它的滚动性能确实很差。 我尝试将 PHImageRequestOptions 选项设置为: let op
我正在开发一个 View Controller 原型(prototype),该原型(prototype)由具有以下要求的 collectionView 组成: 有一张背景图片可见 部分可以像 Acco
我是 Java 的新手,我很好奇 Java 与 C++ 相比如何处理普通任务。因此,我将 2 个 C++ 字符串格式化方法(sprintf 和 ostringstream)与使用 Java 的 Str
我正在为我的VOIP应用程序寻找Java中的自适应 jitter buffer 实现。我为应用程序编写了一个固定的 jitter buffer ,但是由于网络质量差,我遇到了缓冲区不足或缓冲区溢出的问
我要绘制三个组的测量值,每个组有五个观察值。我想绘制所有点,但是每个组中的数值都非常接近。改变 alpha 有帮助,但仍然很难观察到单独的点。 因此我想添加一些水平扩展(在 X 轴上)。点的部分重叠是
我想做的是: a) 让 ggplot 代码每次运行时生成的图都相同 [set.seed 是一种概念吗?] 和 b) 仅对具有相同 y 轴值的标签进行抖动文本标签 - 保留其他文本标签。这似乎是某种基于
我要绘制三个组的测量值,每个组有五个观察值。我想绘制所有点,但是每个组中的数值都非常接近。改变 alpha 有帮助,但仍然很难观察到单独的点。 因此我想添加一些水平扩展(在 X 轴上)。点的部分重叠是
对于 VoIP 语音质量监控应用程序,我需要将传入的 RTP 音频流与引用信号进行比较。对于信号比较本身,我使用预先存在的专用工具。对于其他部分(抓包除外),Gstreamer 库似乎是一个不错的选择
使用 Bootstrap (3.3.6) 折叠,可折叠的 DIV 位于表单输入\标签之间。当 DIV 展开\隐藏时,会出现一种快速\抖动。 问题代码:https://jsfiddle.net/ybto
在我当前的代码库中,复杂的 if 语句通常被委托(delegate)调用所取代。由于代码的结构,在应用程序的过程中将多次调用同一个委托(delegate)。例如, class ExampleClass
我的一个应用程序中的 Gallery 小部件遇到了一个相当奇怪的问题,我想是时候问问其他人了,因为它已经困扰我很长时间了. 我正在为 Gallery 小部件使用自定义 Adapter,并且我在此为 G
我一直在阅读 Eric Lippert的 article关于 C# 编译器的/optimize 命令行选项。文章描述了编译器执行的优化类型。但是,我仍然不清楚此选项是否也会影响 JIT 优化。并非不可
我制作了以下图 block ,带有悬停效果,通过 CSS-transition 增加了 font-size: body { font-family: 'Segoe UI', sans-serif;
如果一个进程被硬件中断(第一级中断处理程序)中断,那么 CPU 调度程序是否意识到这一点(例如,调度程序是否独立于被中断的进程计算硬件中断的执行时间)? 更多详情:我正在尝试解决以下问题:htop 中
如何消除错误 jitter buffer full ? 我使用来自最新快照之一的 ffmpeg。 ffmpeg 版本 N-90078-gf611fef 版权所有 (c) 2000-2018 the F
附上一张照片来解释这个概念,但本质上,我想要一个背景视频或图像,上面有多个 png 像这样分层。 我将如何实现这一目标?我觉得 jit.alphablend 是这个的一个组成部分...... 我也在
我正在更新a package that uses base graphics至 one that uses ggplot2 graphics 。在基本图形版本中,用户可以为 jj 提供一个值,然后该值
我是一名优秀的程序员,十分优秀!