I'm trying to create a game with many levels loaded from XML files. In my game I have many objects in each level.

At present my game contains 20 levels, and I load all the textures at once on startup. But I think the correct way to do it is to only load textures used in the current level. I don't know how to do that. So please explain this by providing some example code.

At present I create a class for each type of entity by extending my Sprite class. This subclass loads the appropriate image. I know this is not the best way to do things.

Basically I want to know how to load large levels efficiently in Andengine. What is the proper method for loading textures, level data and background images from files when the level is run?

  • \$\begingroup\$ You make a function "LoadLevel" and call it once needed? There really is no magic. \$\endgroup\$
    – API-Beast
    Jun 20, 2012 at 13:38
  • \$\begingroup\$ According to loaded level I want to load only that many textures. That was my problem. \$\endgroup\$
    – Siddharth
    Jun 20, 2012 at 14:27
  • 3
    \$\begingroup\$ Well, make two lists, one with the assets already loaded and one with the assets needed by the next level, then you can load the assets which are needed but not loaded and unload the assets that are loaded but not needed. \$\endgroup\$
    – API-Beast
    Jun 20, 2012 at 17:01
  • \$\begingroup\$ I want to reduce the loading time of the game and you say I first load all the resources that thing I was not able to understand. \$\endgroup\$
    – Siddharth
    Jun 21, 2012 at 4:37
  • 1
    \$\begingroup\$ Perhaps I am misunderstanding your question, and thus providing a simplistic answer, but I assume the player is somewhere before your level begins (menu, between level screen, etc), and thus I assume they do something to enter the next level. At whatever point the player moves to the next level, just call a “Load Level” function and pass it the level you want to load. Within that function your logic should only load those resources needed for the next level. I don’t mean to sound sarcastic, but because this should be self-evident I believe I may be interpreting your question incorrectly \$\endgroup\$
    – Pluvius
    Jun 26, 2012 at 22:23

1 Answer 1


What you're looking for is a method to generate the asset dependencies of your individual levels. 'Dependencies' meaning that your levels rely on those files being loaded in order to run properly.

What I would suggest is that your run your XML files through some sort of processing prior to runtime. You can write a process that traverses an XML file and determines what textures it references, and then write the names of those textures either into the same XML file or into a separate 'dependencies file'. At runtime, when you load a level's XML, you will also need to write code to load that level's dependencies.

I can't speak directly to a method to use in Andengine, nor can I provide good example code because the solution will vary depending on how your data is organized and loaded, but this should be a good guideline to get you started.

  • \$\begingroup\$ This is the right answer but there are some more things you need to consider. When you change maps you need a mechanism to identify which textures were used by the previous map, and unload them. If a texture was used by the previous map and is also used by the new map, you don't want to unload it however - just mark it as still active and keep it. Map load times will also get longer as you're now loading textures on demand per-map, but that's a tradeoff vs shorter startup time. \$\endgroup\$ Feb 20, 2013 at 14:16

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .