如何在android中编写滚动游戏摄像头?

发布于 2024-09-24 13:18:12 字数 398 浏览 2 评论 0原文

我目前正在尝试编写一个简单的自上而下视角的射击游戏。我正在使用 SurfaceView 和 Canvas 在屏幕上绘制图形,但我不知道如何使“相机”可滚动。 玩家角色始终位于屏幕中央,并且摄像机(本质上是 SurfaceView 的画布)应该始终跟随他(例如,像《异形群》中那样)。但是,我不知道如何以常规方式实现这一点。我现在的做法是根据玩家的输入移动游戏世界中的所有其他对象 - 如果它们的坐标恰好在屏幕上,则绘制它们。玩家始终保持在相同的坐标上。 有什么方法可以实际移动(滚动)SurfaceView 本身吗?我看到了 Scroll 小部件和 SurfaceView.scrollBy(int x, int y) 方法,但我尝试实现其中任何一个都会导致强制关闭。 感谢任何帮助,提前致谢!

PS 请注意 - 我对在这个项目中使用 open GL 不感兴趣。

I am currently trying to write a simple shoot them up game with top down perspective. I am using SurfaceView and Canvas to draw the graphics on the screen, but I cannot find out how to make the "camera" scrollable.
The player character is always in the center of the screen and the camera(SurfaceView's canvas essentially) is supposed to be following him all the time(for example, like in Alien Swarm). However, I cannot figure out how to make that happen in a regular manner. The way I am doing it right now is by moving every other object in the game world according to the player's input - and drawing them if their coordinates happen to be on screen. The player stays on the same coordinates all the time.
Is there any way to actually move(scroll) the SurfaceView itself? I saw the Scroll widget and the method SurfaceView.scrollBy(int x, int y), but my attempts to implement either of those result in Force Close.
Any help is appreciated, thanks in advance!

P.S. Just a note - I am not interested in using open GL for this project.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

何其悲哀 2024-10-01 13:18:12

正如基里尔在评论中指出的那样,上述答案是不正确的。
要实现一个简单的滚动相机,您需要将

  1. 画布转换为玩家位置的反转坐标
  2. 绘制背景绘制背景
  3. 翻译回(0,0)
  4. 绘制玩家

所以,将其放入代码中

//Translate to -playerX, -playerY and draw the background
canvas.translate(-playerX, -playerY);
background.draw();

//Translate back to 0,0 and draw the player
canvas.translate(playerX, playerY);
player.draw();

这样,玩家始终停留在在屏幕上相同的位置,您将看到背景在其后面移动。

需要注意的一件事是 canvas.translate(x,y) 不会将画布转换为坐标 (x,y)。它按指定的距离平移。一开始这可能会令人困惑。

示例:

  1. 您位于 (0,0)
  2. 您调用 canvas.translate(2,3)
  3. 您调用 canvas.translate(-1,-1)
  4. 您现在位于 (1, 2)

As Kiril pointed out in his comment, the answer above is not correct.
To achieve a simple scrolling camera, you need to

  1. Translate your canvas to the inversed coordinates of the player position
  2. Draw the background
  3. Translate back to (0,0)
  4. Draw the player

So, to put that in code

//Translate to -playerX, -playerY and draw the background
canvas.translate(-playerX, -playerY);
background.draw();

//Translate back to 0,0 and draw the player
canvas.translate(playerX, playerY);
player.draw();

This way, the player always stays at the same position on the screen and you will see the background move behind it.

One thing to note is that canvas.translate(x,y) does not translate the canvas to the coordinates (x,y). It translates by the distance specified. This can be confusing at first.

Example :

  1. You're at (0,0)
  2. You call canvas.translate(2,3)
  3. You call canvas.translate(-1,-1)
  4. You're now at (1, 2)
一影成城 2024-10-01 13:18:12

绘制屏幕内容后,您可以使用翻译来滑动屏幕内容。如果角色要保留在中间,您将编写如下所示的代码:

draw_the_screen();
canvas.translate(x,y);
draw_the_avatar();

翻译时,直到该点的所有内容都将被翻译。当您绘制头像时,平移(滑动)已经发生,并且它将最终到达您绘制的任何地方(即:屏幕的中心)。

希望这有帮助。

You can use Translation to slide the contents of the screen after you've drawn it. If the character is to remain in the middle, you'll write code that looks like this:

draw_the_screen();
canvas.translate(x,y);
draw_the_avatar();

When you translate, everything up until that point will be translated. When you draw the avatar, the translation (sliding) will have already taken place and it'll end up wherever you draw it (i.e.: the center of the screen).

Hope this helps.

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