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

2017/4/3

How to Change background mid game?

crasnoml101 crasnoml101

2017/4/3

#
im trying to change the background image of my games background mid game to my background inverts but im not able to use the set image command? any Suggestions or alternate codes?
Super_Hippo Super_Hippo

2017/4/3

#
You need to use the 'setBackground' method in a World subclass similar to the 'setImage' method in Actor subclasses.
crasnoml101 crasnoml101

2017/4/5

#
I used this code to get it to switch back and forth between between two different backgrounds and it compiles just fine but nothing happens?
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
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
/**
 * Write a description of class MyWorld here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class MyWorld extends World
{
    private int count;
    private GreenfootImage B1;
    private GreenfootImage B2;
 
    /**
     * Constructor for objects of class MyWorld.
     *
     */
    public MyWorld()
    {   
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(950, 600, 1);
        B1 = new GreenfootImage("B.jpg");
        B2 = new GreenfootImage("B2.jpg");
    }
     
    public void act()
    {
        BackgroundSwitch();
        if (Greenfoot.getRandomNumber(100) > 10)
        {
            addObject(new Border(), 950, 580);
            /**
             * lower right
             */
        }
        if (Greenfoot.getRandomNumber(100) > 10)
        {
            addObject(new Border2(), 5, 30);
            /**
             * uper left
             */
        }
        if (Greenfoot.getRandomNumber(100) > 10)
        {
            addObject(new Border3(), 950, 30);
            /**
             * lower left
             */
        }if (Greenfoot.getRandomNumber(100) > 10)
        {
            addObject(new Border4(), 5, 580);
            /**
             * uper right
             */
        }
    }
     
    private void BackgroundSwitch()
    {
        if (count % 20 == 0)
        {
            if (getBackground() == B1)
                setBackground(B2);
            else
                setBackground(B1);
        }
        count++;
    }
danpost danpost

2017/4/5

#
When setting an image to the background, greenfoot creates a new image. That is, using 'getBackground' never returns the same GreenfootImage object that you set to the background -- they might be equivalent, but not the same. This is for when the world set its background -- setting the image of an Actor object will set the same image and 'getImage' will return a reference to that image that was set to it. You can, however, make use of the counter for switching backgrounds:
1
2
3
4
5
6
7
8
9
10
11
private void BackgroundSwitch()
{
    if (count % 20 == 0)
    {
        if (count % 40 == 0)
            setBackground(B1);
        else
            setBackground(B2);
    }
    count++;
}
You need to login to post a reply.