Android——基于异步对象移动的平滑画布平移滚动
我有一个 Android 游戏,球根据加速度计移动。球以可变速度移动。该游戏的关卡比智能手机屏幕大得多,因此当球接近屏幕边缘时我会平移画布以适应大关卡。滚动有效,麻烦的是基于球的平滑滚动。
以下是根据更新之间球的位置变化而变化的尝试:
// RIGHT SHIFT
shiftFactor = Math.abs(xCoor - prevXCoor);
if (ball.x + translateX > rightX * 0.8
&& rightX < levelWidth + padding) {
translateX -= shiftFactor;
leftX += shiftFactor;
rightX += shiftFactor;
}
It looks jittery.这是一个基于某些常数而变化的尝试。
int shiftFactor=2;
//SHIFT SCREEN RIGHT
if (ball.x + translateX > RightPerimeter * 0.8 && rightPerimeter < levelWidth) {
translateX -= shiftFactor;
viewPortLeft += shiftFactor;
viewPortRight += shiftFactor;
}
对于较低的 shiftFactor 值,它会平滑地滚动,但球可以轻松“超过”滚动。对于较大的 shiftFactor,滚动会逐渐变得不稳定。
有什么建议吗?谢谢。
I have an android game where a ball moves based on the accelerometer. The ball moves at a variable speed. The game has levels much larger than a smartphone screen, and so I translate the canvas as the ball nears the edge of the screen to accomodate the large levels. The scrolling works, the trouble is smoothly scrolling based on the ball.
Here's an attempt that shifts based on the change of position of the ball between updates:
// RIGHT SHIFT
shiftFactor = Math.abs(xCoor - prevXCoor);
if (ball.x + translateX > rightX * 0.8
&& rightX < levelWidth + padding) {
translateX -= shiftFactor;
leftX += shiftFactor;
rightX += shiftFactor;
}
It looks jittery.
Here's an attempt that shifts based on some constant.
int shiftFactor=2;
//SHIFT SCREEN RIGHT
if (ball.x + translateX > RightPerimeter * 0.8 && rightPerimeter < levelWidth) {
translateX -= shiftFactor;
viewPortLeft += shiftFactor;
viewPortRight += shiftFactor;
}
For lower shiftFactor values, it smoothly scrolls, but the ball can easily 'outrun' the scrolling. For larger shiftFactors, the scrolling gets incrementally choppier.
Any suggestions? Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你的问题是帧率太低。根据我的经验,Canvas 不具备以良好的帧速率运行实时游戏的能力。如果切换到使用 OpenGL 渲染,您将看到性能的巨大提升。
Your problem is that your frame rate is too low. In my experience Canvas isn't well-equipped to do real-time games at a good frame rate. You'll see a big performance gain if you switch to rendering with OpenGL.