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

2017/5/5

GifImage as a background/world

1
2
dirtyninja1 dirtyninja1

2017/5/5

#
I am trying to have a .gif image as background (moving water). How can i get this to work?
danpost danpost

2017/5/5

#
The GifImage class is only good for animating actor objects. However, you can still use the GifImage class to get the array of GreenfootImages from the gif and then use my Animation class to animate the background of your world. It can be found here.
dirtyninja1 dirtyninja1

2017/5/5

#
danpost wrote...
However, you can still use the GifImage class to get the array of GreenfootImages from the gif .
I dont understand this part
danpost danpost

2017/5/5

#
Import (use menubar -- Edit>Import class...) the GifImage class into your project. Create a GifImage object and use the 'getImages' method to acquire the list of images from the GifImage object. Convert the list to an array and set the array of images to an Animation object. My GifImage vs Animation scenario, which is downloadable to look at, does precisely that in it. It uses the GifImage object to decode the 'gif' into its individual GreenfootImage objects to be used within an Animation object.
Yehuda Yehuda

2017/5/5

#
dirtyninja1 wrote...
I am trying to have a .gif image as background (moving water). How can i get this to work?
You create an instance of the GifImage class (after importation) by doing:
1
GifImage backgroundGif = new GifImage(filename);
Then in your World's act method (and constructor) you put:
1
setBackground(backgroundGif.getCurrentImage());
danpost danpost

2017/5/5

#
Yehuda wrote...
dirtyninja1 wrote...
I am trying to have a .gif image as background (moving water). How can i get this to work?
You create an instance of the GifImage class (after importation) by doing:
1
GifImage backgroundGif = new GifImage(filename);
Then in your World's act method (and constructor) you put:
1
setBackground(backgroundGif.getCurrentImage());
Yeah. I was totally mistaken about only being for Actor objects. Synoptic misfire.
Yehuda Yehuda

2017/5/5

#
danpost wrote...
Yeah. I was totally mistaken about only being for Actor objects. Synoptic misfire.
I'll take that to mean that I was right, but you disturbed me in middle of editing my post and now I lost everything (I can't go back to the page because it just says that something went wrong).
Yehuda Yehuda

2017/5/5

#
If you want to have the animation pause and resume with Greenfoot then you'll have to add the following methods into your World class (with the .gif background):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/**
 * This method is called by the Greenfoot system when the execution has
 * started. This method can be overridden to implement custom behavior when
 * the execution is started.
 */
@Override
public void started() {
    gackgroundGif.resume();
}
 
/**
 * This method is called by the Greenfoot system when the execution has
 * stopped. This method can be overridden to implement custom behavior when
 * the execution is stopped.
 */
@Override
public void stopped() {
    backgroundGif.pause();
}
dirtyninja1 dirtyninja1

2017/5/6

#
Yehuda wrote...
dirtyninja1 wrote...
I am trying to have a .gif image as background (moving water). How can i get this to work?
You create an instance of the GifImage class (after importation) by doing:
1
GifImage backgroundGif = new GifImage(filename);
Then in your World's act method (and constructor) you put:
1
setBackground(backgroundGif.getCurrentImage());
I tried this but i get an error with "getCurrentImage" saying "cannot find symbol - method getCurrentImage"
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
/**
 * Write a description of class Level3 here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class Level3 extends World
{
    /**
     * Constructor for objects of class Level3.
     *
     */
    public Level3()
    {   
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 600, 1);
    }
    private void Animation(){       
        setBackground(backgroundGif.getCurrentImage());
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
/**
 * Write a description of class water here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class backgroundGif extends Actor
{  
    GifImage backgroundGif = new GifImage("c6orUp5K3WhdS.gif");
    /**
     * Act - do whatever the water wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
        setImage( backgroundGif.getCurrentImage() );
    }   
}
danpost danpost

2017/5/6

#
Move line 11 of the backgroundGif class to line 11 of the Level3 class. Then remove the entire backgroundGif subclass of Actor. Finally, add an act method to the Level3 class and call the Animation method from it.
dirtyninja1 dirtyninja1

2017/5/6

#
danpost wrote...
Move line 11 of thee backgroundGif class to line 11 of the Level3 class. Then remove the entire backgroundGif subclass of Actor.
Thanks! but the world class still isnt animated. Did i do something wrong?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public class Level3 extends World
{
    GifImage backgroundGif = new GifImage("c6orUp5K3WhdS.gif");
    /**
     * Constructor for objects of class Level3.
     *
     */
    public Level3()
    {   
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 600, 1);
        Animation();
    }
    public void Animation(){       
        setBackground(backgroundGif.getCurrentImage());
    }
}
Super_Hippo Super_Hippo

2017/5/6

#
Add an act method:
1
2
3
4
public void act()
{
    Animation();
}
dirtyninja1 dirtyninja1

2017/5/6

#
Super_Hippo wrote...
Add an act method:
1
2
3
4
public void act()
{
    Animation();
}
Thanks alot!
danpost danpost

2017/5/6

#
Yehuda wrote...
If you want to have the animation pause and resume with Greenfoot then you'll have to add the following methods into your World class (with the .gif background): < Code Omitted >
Using the 'started' and 'stopped' methods are totally unnecessary. If the scenario is paused, the background image cannot be updated (through the code); and the code to animate the background will again run when the project is restarted.
Yehuda Yehuda

2017/5/7

#
danpost wrote...
Using the 'started' and 'stopped' methods are totally unnecessary. If the scenario is paused, the background image cannot be updated (through the code); and the code to animate the background will again run when the project is restarted.
I thought I remembered that when the execution is paused and then resumed the animation will jump the amount of frames that it would have played had the execution been running. Meaning that the GIF animation still runs, but the code isn't running to update the image to the world so the animation "jumps" (frames).
There are more replies on the next page.
1
2