如何在android中编写滚动游戏摄像头?
我目前正在尝试编写一个简单的自上而下视角的射击游戏。我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
正如基里尔在评论中指出的那样,上述答案是不正确的。
要实现一个简单的滚动相机,您需要将
所以,将其放入代码中
这样,玩家始终停留在在屏幕上相同的位置,您将看到背景在其后面移动。
需要注意的一件事是 canvas.translate(x,y) 不会将画布转换为坐标 (x,y)。它按指定的距离平移。一开始这可能会令人困惑。
示例:
As Kiril pointed out in his comment, the answer above is not correct.
To achieve a simple scrolling camera, you need to
So, to put that in code
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 :
绘制屏幕内容后,您可以使用翻译来滑动屏幕内容。如果角色要保留在中间,您将编写如下所示的代码:
翻译时,直到该点的所有内容都将被翻译。当您绘制头像时,平移(滑动)已经发生,并且它将最终到达您绘制的任何地方(即:屏幕的中心)。
希望这有帮助。
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:
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.