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

2012/5/29

Turret Explosion

SayMyName SayMyName

2012/5/29

#
Hi, I want to make my turret explode when an enemy touches it. So far the explosion will consist of 25 images so I would need an array of it. Is this correct in making an array of pictures?
private GreenfootImage[] turretExplosion; 

        turretExplosion = new GreenfootImage[25];
        //filling the array with pictures
        for (int i = 1; i == 25; i++)
        {
            turretExplosion[i] = new GreenfootImage ("turretExplosion/i");
        }
Also i made the explosion its own class. How would i come about making the explosion when an enemy touches the turret? thanks
nccb nccb

2012/5/30

#
Your code has several issues. Array indexes start at 0, so if your images are named 1, 2, etc, you will need to either subtract one when using the array, or add one when using the image. I'll do the former. The for loop condition should say when to continue executing, so at the moment it won't do anything because it sets i to 1, but only continues while i equals 25. And to join the image number on to the name, it has to be outside the string:
        for (int i = 1; i <= 25; i++)  
        {  
            turretExplosion[i - 1] = new GreenfootImage ("turretExplosion/" + i + ".png");  
        }  
I've guessed that your files are PNG files, but adjust the extension as necessary. So now if you have your files in <your scenario directory>/images/turretExplosion/1.png, this code will pick them up. For the next step, you'll need to keep track of which image you're showing using an integer, and do something like this:
setImage(turretExplosion[curIndex]);
curIndex += 1;
SayMyName SayMyName

2012/5/31

#
Thank you for your help, it works now.
You need to login to post a reply.