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

2015/1/24

Removing object with a timer

C.C.S C.C.S

2015/1/24

#
So I have to remove the current actor when the gif animation ends. This is what I have at this moment.
import greenfoot.*;

public class Explosion extends Starter
{
    private int minExplosionTime = 5;
    private int ExplosionTime = 0;
    private GifImage Img = new GifImage("Explosion.gif");
    public void act() 
    {
        setImage(Img.getCurrentImage());
        ExplosionTime++;
        if (ExplosionTime >= minExplosionTime)
        {
            ExplosionTime = 0;
            getWorld().removeObject(this);
        }
    }
}
Any suggestions on how I can do this?
danpost danpost

2015/1/25

#
I will presume you are using the importable GifImage class that comes with greenfoot. This class gives your actor an animation that constantly repeats (after the last image is used, it goes back to the first one automatically). The class does not provide a way to run exactly one cycle of images. So, checking for moment when the last image has expired its display time would be the best way to go. This would be when the image set to the actor is the last image in the GifImage list of images ( 'Img.getImages()' ) and the image returned from 'getCurrentImage()' is the first image.
C.C.S C.C.S

2015/1/25

#
So basically add a check variable that gets all the images and if the last image has been displayed remove the actor. Is that right? Or I could create a blank .png image that gets set when the last image from the gif animation has finished. Bit world it work?
danpost danpost

2015/1/25

#
C.C.S wrote...
So basically add a check variable that gets all the images and if the last image has been displayed remove the actor. Is that right?
Not quite. Just checking for the last image will not give it time to display. You need to check for the one act cycle when the last image is displaying and the actors image is changed to the first image. That means that before setting the image to what is returned by 'getCurrentImage', you need to see if the Actor object method 'getImage' returns the last image in the List object returned by the GifImage object method 'getImages'. Then, if it is and 'getCurrentImage' returns the first image in the list, you can remove the actor.
Or I could create a blank .png image that gets set when the last image from the gif animation has finished. Bit world it work?
By doing that, you could eliminate one of the checks. You would not need to see if the current image is the last one; but, instead you would be checking if the new image being set is the last one.
C.C.S C.C.S

2015/1/26

#
OK got it now. Thank you very much.
You need to login to post a reply.