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

2021/1/10

Collecting all variables of a type in an array

FirewolfTheBrave FirewolfTheBrave

2021/1/10

#
In one of the classes for my new project, I use several different images to create the animation, each one stored in a variable of the type GreenfootImage. Now, I want to write a method that uses all images one by one, and to do that, I'd like to have a for-loop iterate through an array instead of doing it manually. Is there a simple way of creating such an array?
Super_Hippo Super_Hippo

2021/1/10

#
Do you mean something like this?
private GreenfootImage[] images =
{
    new GreenfootImage("image0.png"),
    new GreenfootImage("image1.png"),
    new GreenfootImage("image2.png")
    //...
}

//...
//in the method
for (int i=0; i<images.length; i++)
{
    image[i].<do something>;
}
danpost danpost

2021/1/11

#
Super_Hippo wrote...
Do you mean something like this? << Code Omitted >>
Line 13 should be:
images[i].<do something>;
However, you will not be able to use a for loop like this:
for (int i=0; i<images.length; i++)
{
    setImage(images[i]);
}
as only the last image will then show. The iterator needs to be an instance field. Also, you do want to change the image every act frame as neither the eye nor the graphic device will be able to keep up with the changes. So, you will need a constant value to control the delay between image changes:
private static final int IMG_DELAY = 10;

private GreenfootImage[] images =
{
    // as above
}
private int iterator;

// in method
iterator = (iterator+1)%(images.length*IMG_DELAY);
if (iterator%IMG_DELAY == 0)
{
    setImage(images[iterator/IMG_DELAY)]);
}
FirewolfTheBrave FirewolfTheBrave

2021/1/11

#
That seems to work, however, it's not exactly what I was looking for. I know how to write the loop iterating through the array, I was just looking for a method that can automate the creation of the array itself. I already have all images saved in a variable, but there's this one method for which I need them in an array, and since it's gonna be quite long, it would be nice to be able to automatically collect all variables of the type GreenfootImage into an array. Does such a method exist?
danpost danpost

2021/1/11

#
FirewolfTheBrave wrote...
That seems to work, however, it's not exactly what I was looking for. I know how to write the loop iterating through the array, I was just looking for a method that can automate the creation of the array itself. I already have all images saved in a variable, but there's this one method for which I need them in an array, and since it's gonna be quite long, it would be nice to be able to automatically collect all variables of the type GreenfootImage into an array. Does such a method exist?
If your image file names are sequential (that is, like "image0.png", "image1.png", "image2.png", etc.), then:
private GreenfootImage[] images = getImages();

private GreenfootImage[] getImages()
{
    GreenfootImage[] imgs = new GreenfootImage[8]; // adjust image count to appropriate value
    for (int i=0; i<imgs.length; i++)
    {
        imgs[i] = new GreenfootImage("image"+i+".png");
    }
    return imgs;
}
You need to login to post a reply.