This site requires JavaScript, please enable it in your browser!
Greenfoot back
manish1
manish1 wrote ...

2013/9/20

help with arrays

manish1 manish1

2013/9/20

#
hello! i am currently working on a project for my school and we are learning about arrays. I decided to use arrays to spawn a object in a randomly selected area. I was wondering if it was possible and if its not i was wondering what arrays are mainly used for in games
danpost danpost

2013/9/20

#
Arrays are mainly used for the arrangement of things (like a chessboard, or grid-layout) and for listing things (anything from ints to objects). For some examples: a series of scores for the different levels in a game; the layout of the levels; a list of objects, colors, or values to be randomly chosen from or for each item to be used for a specific purpose (such as the x and y coordinates of a polygon; where an array of x values and an array of y values are used as argument in the 'drawPolygon(int, int, int)' and 'fillPolygon(int, int, int)' methods of the GreenfootImage class.
Zamoht Zamoht

2013/9/20

#
Arrays works like the List class (but is simpler and doesn't have any methods). Arrays are used if you need a collection of objects. An example of this would be making an animation for one of your actors. You would then create a new array like this I think (please correct me if I'm wrong): GreenfootImage images = new GreenfootImage; This creates an array containing 5 GreenfootImage objects. Now a setup of the array would look like this for instance: images = new GreenfootImage("image1"); images = new GreenfootImage("image2"); And so on (this is not a recommended setup for large arrays). Now to run the animation you want a counter and the code would look something like this: private int counter = 0;
    public void act()
    {
        setImage(images[counter++]);
        counter %= 5;
    }
Hope this makes sense. And as always danpost has faster response.. :)
You need to login to post a reply.