0
\$\begingroup\$

I have a problem with my Sprites. I'm trying to create a lot of Sprites. However, I got a problem in that I have several grass tiles and I don't want to do this

grass1 = new Sprite...; grass2 = new Sprite..;

The problem is I make an array of Sprites say Sprite[] grass = new Sprite[7]; I don't know how I would intialize them. I need to put

for(int i=0; i<grass.length;i++){
        grass[i] = new Sprite(16,0,i,SpriteSheet.tiles);
    }

Somewhere, however I get an error saying you have to put it in a bracket. I get a syntax error. I'm thinking that I should put it into the constructor so I have

    public Sprite(){
    for(int i=0; i<grass.length;i++){
            grass[i] = new Sprite(16,0,i,SpriteSheet.tiles);
         }
    }

However, I think that is bad for memory. I don't know how to solve this. I think I need to use an array list. Maybe, create an array list of grass and then add elements in it.

   public class Sprite {
        public final int SIZE;
        private int x;
        private int y;
        public int[] pixels;
        private SpriteSheet sheet;

        public static Sprite[] grass = new Sprite[7];
        for(int i=0; i<grass.length;i++){
            grass[i] = new Sprite(16,0,i,SpriteSheet.tiles);
        }

        public static Sprite voidTile = new Sprite(16,0xffffff);

        public static Sprite player_up = new Sprite(32,0,5, SpriteSheet.tiles);
        public static Sprite player_back = new Sprite(32,2,5, SpriteSheet.tiles);
        public static Sprite player_right = new Sprite(32,1,5, SpriteSheet.tiles);
        public static Sprite player_left = new Sprite(32,3,5, SpriteSheet.tiles);

        public static Sprite player_up1 = new Sprite(32,0,6, SpriteSheet.tiles);
        public static Sprite player_back1 = new Sprite(32,2,6, SpriteSheet.tiles);
        public static Sprite player_right1 = new Sprite(32,1,6, SpriteSheet.tiles);
        public static Sprite player_left1 = new Sprite(32,3,6, SpriteSheet.tiles);

        public static Sprite player_up2 = new Sprite(32,0,7, SpriteSheet.tiles);
        public static Sprite player_back2 = new Sprite(32,2,7, SpriteSheet.tiles);
        public static Sprite player_right2 = new Sprite(32,1,7, SpriteSheet.tiles);
        public static Sprite player_left2 = new Sprite(32,3,7, SpriteSheet.tiles);

        public Sprite(int size, int x, int y, SpriteSheet sheet){
            this.SIZE=size;
            pixels = new int[SIZE*SIZE];
            this.x=x*SIZE;
            this.y=y*SIZE;
            this.sheet = sheet;
            load();
        }

        public Sprite(int size, int colour){
            SIZE=size;
            pixels= new int[size*size];
            setColour(colour);
        }

        private void setColour(int colour) {
            for(int i=0; i<SIZE*SIZE; i++){
                pixels[i]=colour;
            }
        }

        private void load() {
            for(int y=0; y<SIZE; y++){
                for(int x=0; x<SIZE; x++){
                    pixels[x+y*SIZE]=sheet.pixels[(this.x+x)+(this.y+y)*sheet.SIZE];
                }
            }

        }


    }
\$\endgroup\$
1
  • \$\begingroup\$ You should always tag your questions with the programming language you're using (assuming it's relevant to the question, e.g. a code issue). I assumed this is C#, but it might be Java as well? \$\endgroup\$
    – Mario
    May 19, 2013 at 8:52

1 Answer 1

0
\$\begingroup\$

I assume you're using C#. Even if you're using Java your problem lies here:

public static Sprite[] grass = new Sprite[7];
for(int i=0; i<grass.length;i++){
    grass[i] = new Sprite(16,0,i,SpriteSheet.tiles);
}

This for() loop is outside any function, and as such not acceptable.

You can't initialize array members that way for static members (or any other members).

If you're using Java, you can use the following code for inspiration, but it won't work 1:1.

There might be better methods to do this in newer versions of C#/.NET, but I'd just create some initializer code called in every constructor (this will work in any version of .NET):

private void Initialize() {      
    static bool init_done = false;
    if (!init_done) {
        for(int i=0; i<grass.length;i++){
            grass[i] = new Sprite(16,0,i,SpriteSheet.tiles);
        init_done = true;
    }
}

The static boolean value will keep the code from running each and every call.

I think .NET version 3 or so introduced the concept of static constructors, which would be the perfect choice for this, but you might not have access to it:

static Sprite() {
    for(int i=0; i<grass.length;i++){
        grass[i] = new Sprite(16,0,i,SpriteSheet.tiles);
}

This code will run once when static members are (have been) initialized.

Essentially, keep in mind that your variable declaration ends after the ;. There must not be any other code other than other declarations following on that Level.

\$\endgroup\$

You must log in to answer this question.

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