Java滚动背景 [英] Java Scrolling Background

查看:180
本文介绍了Java滚动背景的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你如何在Java中滚动背景,使它看起来像一个精灵正在移动?我正在使用Applet。有没有比让游戏中的所有对象向左或向右移动更有效的方法?

How would you scroll the background in Java, to make it look like a sprite is moving? I am using an Applet. Is there a more efficient way than making all the objects in the game move left or right?

推荐答案

大多数游戏重绘整个背景每一帧,然后在其上重绘所有精灵。

Most games redraw the entire background every frame, then redraw all sprites on top of it.

如果你的背景只是一个大图像,那么这是非常有效的(因为内存 - >内存图像副本非常快。)

If your background is just a big image, then this is surprisingly efficient (since a memory-> memory image copy is very fast).

为了保持在2D游戏中滚动的感觉:

To maintain the perception of scrolling in a 2D game:


  • 定义一对坐标(scrollX,scrollY),用于存储屏幕左上角的当前滚动位置。

  • 只要你想滚动(例如跟随玩家),你就可以更新scrollX和ScrollY。

  • 给每个精灵一个坐标(spriteX,spriteY) )相对于游戏世界(即游戏背景)和相对于屏幕位置的。滚动后你不需要改变这些数字....

  • 然后你需要在屏幕位置(-scrollX,-scrollY)和每个精灵()处绘制背景( spriteX-scrollX,spriteY-scrollY)。

  • Java应该为您处理屏幕剪辑。但是如果您的游戏世界和背景很大,那么您可能需要一些额外的技术来避免尝试绘制屏幕外对象的开销(这在游戏开发术语中将被称为视图截顶剔除)

  • Define a pair of coordinates (scrollX, scrollY) that store the current scroll position of the top left of the screen.
  • You can update scrollX and ScrollY whenever you want to scroll (e.g. following the player)
  • Give each sprite a co-ordinate (spriteX, spriteY) that is relative to the game world (i.e. the game background) and not relative to the screen position. Then you don't need to change these numbers when you scroll....
  • Then you need to draw the background at screen position (-scrollX, -scrollY) and each sprite at (spriteX-scrollX, spriteY-scrollY).
  • Java should take care of the screen clipping for you. But if your game world and background is large, then you may need some additional techniques to avoid the overhead of trying to draw offscreen objects (this would be called "view frustrum culling" in game development lingo)

这篇关于Java滚动背景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆