安卓游戏背景

发布于 2024-12-28 11:55:47 字数 399 浏览 2 评论 0原文

所以我正在为一个学校项目开发一个游戏,到目前为止进展顺利,但我一直在尝试使用位图图像而不是仅使用drawColor作为背景,但这样做使游戏变得迟缓,没有响应到触摸事件,最终在一段时间后崩溃。这是到目前为止我制作不断崩溃的背景的代码,我在drawBitmap之前使用getColor来清空以前的背景,因为如果没有这个,移动的字符线程会留下“踪迹”,并且在每次移动后都不会被擦除。

    canvas.drawColor(Color.BLACK);
    canvas.drawBitmap
    (BitmapFactory.decodeResource(getResources(),R.drawable.park),0,0,null);

因此,任何更有效的方法来制作静态背景图像以供线程移动,我们将不胜感激!

So I'm working on developing a game for a school project and so far its gone well, but I have been trying to use a bitmap image instead of just using drawColor for the background, but doing so has make the game sluggish, not respond to to Touch Events, and ultimately crash after a while. Here is my code so far for making that background that keeps crashing, I am using the getColor before the drawBitmap as to blank out the previous background, because without that the moving character threads were leaving a "trail" and not being erased after every movement.

    canvas.drawColor(Color.BLACK);
    canvas.drawBitmap
    (BitmapFactory.decodeResource(getResources(),R.drawable.park),0,0,null);

So any more effective method to make a static background image for the thread to move over would be appreciated!

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

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

发布评论

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

评论(2

温柔少女心 2025-01-04 11:55:48

您所做的是每次渲染时都加载位图,这就是为什么它运行缓慢并在一段时间后由于内存限制而崩溃的原因。您应该在初始化所有内容时加载图像一次:

// run once when you start the game
Bitmap background = BitmapFactory.decodeResource(getResources(),R.drawable.park);

在渲染代码中:

canvas.drawBitmap(background);

并且在完成后不要忘记释放图像:

background.recycle();

What you are doing is that you are loading your bitmap everytime you're rendering, which is why it runs slow and crashes after a while because of memory constraints. You should load your image once when you initialize everything:

// run once when you start the game
Bitmap background = BitmapFactory.decodeResource(getResources(),R.drawable.park);

In your render code:

canvas.drawBitmap(background);

And don't forget too free the image when you're done with it:

background.recycle();
沧桑㈠ 2025-01-04 11:55:48

您的应用程序变慢的原因是您保留了太多对同一位图的引用,这消耗了可用的内存量,您需要释放图像,因为您不需要它们。 Romain Guy 制作了一系列有关图形和游戏图形的视频。看看 youtube,你就会知道如何处理这个问题。

The reason your application is slowing down is you are keeping too many references to the same bitmap and this is consuming the amount of memory that you have available, you need to release images as you dont need them. There a bunch of videos by Romain Guy on graphics and game graphics. Look on youtube and you'll learn how to handle this problem.

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