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

So I'm trying to load various different types of grass Sprites from the same spritesheet. However, the code looks really ugly. The thing is I have

public static Sprite grassi = new Sprite(16,i-1,0,SpriteSheet.tiles);

Now I was wondering is there a way to put this into a for loop. As I currently, have this mess below

public static Sprite grass1 = new Sprite(16,0,0,SpriteSheet.tiles);
public static Sprite grass2 = new Sprite(16,1,0,SpriteSheet.tiles);
public static Sprite grass3 = new Sprite(16,2,0,SpriteSheet.tiles);
public static Sprite grass4 = new Sprite(16,3,0,SpriteSheet.tiles);
public static Sprite grass5 = new Sprite(16,4,0,SpriteSheet.tiles);
public static Sprite grass6 = new Sprite(16,5,0,SpriteSheet.tiles);
public static Sprite grass7 = new Sprite(16,6,0,SpriteSheet.tiles);
public static Sprite grass8 = new Sprite(16,7,0,SpriteSheet.tiles);
public static Sprite grass9 = new Sprite(16,8,0,SpriteSheet.tiles);
public static Sprite grass10 = new Sprite(16,9,0,SpriteSheet.tiles);
public static Sprite grass11 = new Sprite(16,10,0,SpriteSheet.tiles);
public static Sprite grass12= new Sprite(16,11,0,SpriteSheet.tiles);
public static Sprite grass13 = new Sprite(16,12,0,SpriteSheet.tiles);
public static Sprite grass14 = new Sprite(16,13,0,SpriteSheet.tiles);
public static Sprite grass15 = new Sprite(16,14,0,SpriteSheet.tiles);
public static Sprite grass16 = new Sprite(16,15,0,SpriteSheet.tiles);

This works, but it destroys the code as it's a big mess. I also have 16x16 SpriteSheet and don't want to do this for every sprite.

share|improve this question
Why don't you use an array to store your different grass types? Provides you an easier access i.e. grass[10]->Dosomething();. Initializing would be easier too: for(int i = 0; i != nGrassTypes; i++) grass[i] = new Sprite(16,i,0,SpriteSheet.tiles); – bryan226 Dec 23 '12 at 0:10
bryan226 Thanks. I have to change some of the code so it access array now instead of position. But, that works. – pangaea Dec 23 '12 at 0:36

A decent option for sprite lookup is the combination of a HashMap with enums. You have an enum to define the type and the HashMap holds the lists of different types. For example:

public enum SpriteType {

HashMap<SpriteType, ArrayList<Sprite>> spritesByType = 
    new HashMap<SpriteType, ArrayList<Sprite>>();

Then for adding all your sprites you can do something like this:

for(SpriteType st : SpriteType.Values()) {
    int spriteCount = getSpriteCountForType(st);
    int spriteColumn = getSpriteColumnForType(st);
    spritesByType.put(st, new ArrayList<Sprite>());
    for(int i = 0; i < spriteCount; i++) {
        spritesByType.get(st).add(new Sprite(spriteColumn,i,0,SpriteSheet.tiles));

Then accessing is easy too:

randomGrassSprite = 
share|improve this answer

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.