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

2014/6/1

Animated Gif Image

QWERTY894 QWERTY894

2014/6/1

#
How do I make an actor with an animated gif image? I have a separate GifImage class but I'm not sure how to use it.
davmac davmac

2014/6/1

#
Declare a GifImage field variable in your actor class, and set it to the appropriate .gif image:
1
GifImage gifImage = new GifImage("mygif.gif");
In the act() method of your actor, set the actor image to the GifImage's current image:
1
setImage(gifImage.getCurrentImage());
QWERTY894 QWERTY894

2014/6/1

#
That's what I have so far but I'm not sure how to use the GifImage class. Right now I have the getCurrentImage() method just returning an image but there is still no animation. Does it need to store an array of images?
davmac davmac

2014/6/1

#
That's what I have so far but I'm not sure how to use the GifImage class
I thought I'd just explained how to use it, above!
Right now I have the getCurrentImage() method just returning an image but there is still no animation. Does it need to store an array of images?
If you just do what I wrote it should work. If it doesn't, maybe you should post your code...
QWERTY894 QWERTY894

2014/6/2

#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
public class Monkey extends Actor
{
    private String img;
    private GifImage gif;
    public Monkey( String img )
    {
        this.img = img;
        gif = new GifImage( img );
    }
    public void act()
    {
        setImage( gif.getCurrentImage() );
    }
}
And here's what I have for the GifImage class
1
2
3
4
5
6
7
8
9
10
11
12
public class GifImage 
{
    private String img;
    public GifImage( String img )
    {
        this.img = img;
    }
    public GreenfootImage getCurrentImage()
    {
        return new GreenfootImage( img );
    }
}
davmac davmac

2014/6/2

#
Ah, I see where the confusion was now. You do not write the GifImage class yourself, you import it. Edit - Import class... - then choose GifImage and click "Import". You should delete your own GifImage class first.
QWERTY894 QWERTY894

2014/6/2

#
Ohhh okay thanks so much! That works!
You need to login to post a reply.