Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

For example, lets say we have an enemy class in my Android game project. I am initializing the enemy bitmap to be used with certain sprites in my game loops init. I am then calling the respective classes init, which is passed this loaded bitmap.

What is the process associated with initializing resources and classes in the game loop?

If I am totally off course just point me in the right direction.

Thanks! :D

share|improve this question
up vote 2 down vote accepted

Whenever I question myself questions like this I try to look at existing examples. In about every game you get a loading screen before the actual game starts -> the loading starts before the actual has started. So all the loading should preferably happen before the gameloop.

When your running a game and a new object is created, which also needs a new resource(let's say a png file for a sprite). You could refer to the already loaded file to prevent loading at run time. Which files should be loaded before beginning a level should be defined somewhere in your level data.

There are scenarios where you are unsure which assets you will use though (dynamically created world for example). I use the following method for this:

[pseudo code]

Class ContentManager()
       Array assets = new Array();

       public static Sprite getSprite(string spritename ){ //or sprite id or whatever unique identifier

           for (Sprite sprite : assets){

               if( == spritename){

                   return sprite;

           //it's not found in the list, so let's load it
           Sprite newsprite = Loader.load(spritename);
           return newsprite;

In short: add every loaded asset to an array. When you need to use an asset, check if it's in the list. Return the sprite if it's there, or load it and add it to the array if it's not found.

share|improve this answer
Would it be inefficient to loop through an array of Assets every time you would like to use that asset? Or would I only access each asset once? – KRB Sep 8 '11 at 16:19
You shouldnt worry about that. But you also shouldnt be constantly querying a resource msnager like that. – dcousens Sep 8 '11 at 23:04

Initialization should not happen in the game loop, you'd end up loading the same resource over and over again.

For a resource like an image associated with a class you should make a static field for holding the reference to the resource. You must load the resource only once. You could do this separately, or you could check during instance initialization if the resource has been loaded or not and then load it if need be.

share|improve this answer
Hey man I just realized (I am just getting back into my game code) that my init isn't called by my thread that is what is "actually" keeping track of the game time. It's only calling onDraw() and onUpdate(). My init() is only called once in this case. You mentioned that I should make a static field for holding the resource. Should this field be in the class it is associated with? Also when I create my surface for the game I initialize all my drawable classes. (is this a good idea?) – KRB Sep 8 '11 at 15:46
Just to be clear, there are countless ways of organizing such stuff, other ways ain't necessarily wrong or worse. But yes, make it a static in the class it is associated with. As long as you don't run out of memory it is usually a good idea to initialize everything before starting the game. – aaaaaaaaaaaa Sep 8 '11 at 16:06

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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