I am trying to build an RPG tile-map based game. Currently I am going to set enemy sprites that will be repeating their motion in fixed path. Suppose that I have a level map, which has got width 3 times greater than actual applet visible to user.

Now my question is that how should I handle creation of enemy sprites? I have got two options in my mind:

1) Create all enemies at a time on level load (I don't want to follow this approach as it will take up extra memory).

2) Create enemies till the level map is visible to user. Load new enemies as user goes further. I want to follow this one but I don't have any idea.

Can someone give me hints to follow this step and tell me approach usually followed by game developers?


Usually you would define an area, which surrounds the current view of the user and is somewhat larger (e.g. 2 or 3 times larger). At startup you load all the enemies in that area. As the user moves you can preload enemies, that enter this area and unload enemies that leave it. This way you have a somewhat upper limit of enemies that need to be loaded and can create arbitrary big worlds.

The key here then is to find the exact size of that area, so that the user never sees an enemy popping up in the current view (that is the reason why simply using the viewport does not suffice). If the enemies move rather slow, the area can be smaller. On the other hand, if enemies can move at high speeds, the area needs to be larger.



Viewports are essentially virtual cameras cameras that look at your scene (2D or 3D). Viewports are always designated by a view matrix, a set of coordinates for location and rotation of the camera (3x3 in 2D and 4x4 in 4D). Viewports allow you to only view a section of the scene. Often times the draw API culls (does not render) anything outside of the viewport (does not draw what it cannot see). For your application you can let the API handle this. More advanced applications use something called Frustum Culling, which allows more control over what is not rendered.

Viewports are an integral part of openGL and DirectX. You did not mention if you are directly using these or an API like LWJGL. Either way, they all have access to viewports.

Also since you are looking at a 2D plane, you need an orthographic presentation. In a 3D world, the distance between a 2D plane and the camera is greater at the edges of view than the center. (If the camera is a pyramid, the distance between the top and a base edge is greater than the distance between the top and the center of the base.) In an orthographic presentation all points on a 2D plane are equidistant from the camera.

  • \$\begingroup\$ Thanks for your suggestion. But I am not really using any of the API. I am just developing game in Eclipse. \$\endgroup\$ – NightFury Aug 26 '13 at 5:16
  • \$\begingroup\$ You are using something to draw sprites. Even openGL is an API. \$\endgroup\$ – mobo Aug 26 '13 at 5:32
  • \$\begingroup\$ I am simply using java up till now making everything work by without any API. I am developing game on java applet. \$\endgroup\$ – NightFury Aug 26 '13 at 5:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.