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

2017/12/6

Animated Gif

suzbo suzbo

2017/12/6

#
I would like to put in an animated gif file to my world class background. What is the code that will do that? Thank you -
danpost danpost

2017/12/6

#
Try using the 'GifImage' class provided with the greenfoot download.
suzbo suzbo

2017/12/7

#
I did thank you. Now I need to know how to use the isRunning() method so that I can make the background animated gif work.
danpost danpost

2017/12/7

#
The 'isRunning' method only returns a true/false value that indicates whether the animation is currently running or paused. It is not used to run the animation. Setting the world background to the image returned by the 'getCurrentImage' is what runs the background animation. After another quick glance at the class, I am surprised at the number of 'java.awt' classes that it imports. I know that the 'java.awt.Color' import will probably need removed. Also, I am with the understanding that the 'java.awt' package (specifically to the GifImage class, referring to the other 'java.awt' classes that are imported) was not going to work with the HTML 5 version of an uploaded scenario .
suzbo suzbo

2017/12/13

#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
/**
 * Space is the setting for Spaceship scenario.
 * This is a place where Meteors, Satelites, and FuelCells float around.
 *
 * @author Michael V.
 * @version 1.0
 */
public class Space extends World
{
    private int fuel;
    private int score;
    /**
     * Constructor: Set up the staring objects.
     */
    public Space()
    {
        super(780, 360, 1);
        prepare();
        setPaintOrder(Border.class);
        score = 0;
        fuel = 2000;
        showFuel();
        //getCurrentImage();
         
    }
 
    /**
     * Create new floating objects at random intervals.
     */
    public void act()
    {
        if (Greenfoot.getRandomNumber(100) < 3)
        {
            addObject(new FuelCell(), 779, Greenfoot.getRandomNumber(360));
        }
        if (Greenfoot.getRandomNumber(100) < 3)
        {
            addObject(new Surface(), 779, 0);
        }
        if (Greenfoot.getRandomNumber(100) < 3)
        {
            addObject(new Surface(), 779, 359);
        }
        if (Greenfoot.getRandomNumber(100) < 1)
        {
            addObject(new Meteor(), 779, Greenfoot.getRandomNumber(360));
        }
        if (Greenfoot.getRandomNumber(100) < .05)
        {
            addObject(new Satelite(), 779, Greenfoot.getRandomNumber(360));
        }
        countFuel();
    }
    public void addScore(int points)
    {
        score = score + points;
        showText("Score: " + score, 80, 25);
    }
    public void addFuel(int bonus)
    {
        fuel = fuel + bonus;
        showText("Fuel: " + fuel, 700, 25);
    }
     
    private void countFuel()
    {
        fuel--;
        showFuel();
        if (fuel == 0)
            {
                showEndMessage();
                Greenfoot.stop();
            }
    }
 
         
    private void showFuel()
    {
        showText("Fuel: " + fuel, 700, 25);
    }
    /**
     * Prepare the world for the start of the program. In this case: Spawn
     * a Spaceship and the moons surface at the edge of screen.
     */
    private void prepare()
    {
        Spaceship spaceship = new Spaceship();
        addObject(spaceship, 103, 179);
        Surface surface = new Surface();
        addObject(surface, 126, 1);
        Surface surface2 = new Surface();
        addObject(surface2, 342, 5);
        Surface surface3 = new Surface();
        addObject(surface3, 589, 2);
        Surface surface4 = new Surface();
        addObject(surface4, 695, 5);
        Surface surface5 = new Surface();
        addObject(surface5, 114, 359);
        Surface surface6 = new Surface();
        Surface surface7 = new Surface();
        addObject(surface7, 295, 353);
        Surface surface8 = new Surface();
        Surface surface9 = new Surface();
        Surface surface10 = new Surface();
        addObject(surface10, 480, 358);
        Surface surface11 = new Surface();
        addObject(surface11, 596, 359);
        Surface surface12 = new Surface();
        addObject(surface12, 740, 354);
        Border border = new Border();
        addObject(border, 0, 180);
        Border border2 = new Border();
        addObject(border2, 770,180);
        setPaintOrder(Border.class);
    }
    private void showEndMessage()
    {
        showText("Times up - you win!", 390, 150);
        showText("Your final score was: " + score + "points!", 390, 170);
    }
     
}
I am not sure where the code should be placed. Can you help me with this?
danpost danpost

2017/12/13

#
Along with your fields declared on lines 12 and 13, add a GifImage reference field and assign a GifImage object given your gif file:
1
private GifImage animation = new GifImage("<< gif filename >>");
The gif file should be in the 'images' folder of the project folder. Line 25 should be:
1
setBackground(animation.getCurrentImage());
This line initializes your world background image. The same line of code should also be in the 'act' method of the class to run the animation when the scenario is running.
You need to login to post a reply.