Android——基于异步对象移动的平滑画布平移滚动

发布于 2024-11-04 01:51:31 字数 821 浏览 0 评论 0原文

我有一个 Android 游戏,球根据加速度计移动。球以可变速度移动。该游戏的关卡比智能手机屏幕大得多,因此当球接近屏幕边缘时我会平移画布以适应大关卡。滚动有效,麻烦的是基于球的平滑滚动。

以下是根据更新之间球的位置变化而变化的尝试:


// RIGHT SHIFT
shiftFactor = Math.abs(xCoor - prevXCoor);
if (ball.x + translateX &gt rightX * 0.8
 && rightX &lt 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

暮年慕年 2024-11-11 01:51:32

你的问题是帧率太低。根据我的经验,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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文