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

2016/11/27

Change background on collision

DukeFutzwillus DukeFutzwillus

2016/11/27

#
I am trying to get it so that when the player object runs into a portal the background image the level switches. All objects are gone and a new screen appears. I need the player to be able to go through a first portal to get to the "equipment screen" and then go through a second portal to get back to the game. The second portal essentially starts the next level. At the moment, I can get the first portal to work the very first time I load up the scenario and then after that when you run the scenario the background image switches immediately without running into the portal at all. This is the code for the first Portal.
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
public class Portal extends SmoothMovement
{
    public static boolean newLevel = false;
    /**
     * Act - do whatever the Portal wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
         
        teleport();
    }   
     
    public void teleport()
    {
        Sworde a = (Sworde) getOneIntersectingObject(Sworde.class);
        if (a != null)
        {
            newLevel = true;
             
        }
        else
        {
            newLevel = false;
        }
    }
This is currently the world code.
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
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;
/**
 * Write a description of class MyWorld here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class MyWorld extends World
{
 
    public  static boolean nextLevel = false;
    public static boolean newLevel = false;
    public int levelSelect = 0;
     
    GreenfootImage planet = new GreenfootImage("Background for ProjectGame.png");
    /**
     * Constructor for objects of class MyWorld.
     *
     */
    public MyWorld()
    {   
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(1000, 600, 1);
       
        prepare();
         
    }
 
    public void act()
    {
        setNewLevel();
        setNextLevel();
     interlude();  
     nextLevelGet();
    }
     
 
    /**
     * Prepare the world for the start of the program.
     * That is: create the initial objects and add them to the world.
     */
    private void prepare()
    {
        Portal.newLevel = false;
        CavernTank caverntank = new CavernTank();
        addObject(caverntank,396,193);
        Sworde sword = new Sworde();
        addObject(sword,118,113);
        Portal portal = new Portal();
        addObject(portal,350,89);
        setBackground(planet);
 
        Bug1 bug1 = new Bug1();
        addObject(bug1,224,124);
         
        setPaintOrder(Sworde.class, TankBoss.class, Bug1.class);
    }
     
    public void interlude()
    {
        if (newLevel = true)
        {
            
           setBackground("Backtobase.png");
           PortaltoPlanet portal = new PortaltoPlanet();
           addObject(portal, 900, 550);
           newLevel = false;
            
        }
    }
     
    public void nextLevelGet()
    {
        if (nextLevel)
        {
        if (levelSelect == 0)
        {
            setBackground(planet);
            levelSelect++;
        }
    }
    }
             
    public void setNewLevel()
    {
        newLevel = Portal.newLevel;
    }
     
    public void setNextLevel()
    {
        nextLevel = PortaltoPlanet.nextLevel;
    }
     
    private void levelOne()
    {
        setBackground("Background for ProjectGame.png");
        setPaintOrder(Sworde.class, TankBoss.class, Bug1.class);
    }
Any help is greatly appreciated. I am really not sure why this is is not working.
DukeFutzwillus DukeFutzwillus

2016/11/27

#
Also, when I remove the interlude() method the bckground image doesnt change immediately upon run
danpost danpost

2016/11/27

#
DukeFutzwillus wrote...
I can get the first portal to work the very first time I load up the scenario and then after that when you run the scenario the background image switches immediately without running into the portal at all.
Fields that are declared 'static' are not re-initialized when the project is reset -- only when the project is compiled. You need to initialize it in your initial world constructor. Place the following at line 25 in your MyWorld class:
1
Portal.newLevel = false;
DukeFutzwillus DukeFutzwillus

2016/11/27

#
Okay, I have done this and the problem still persists. Again, when I remove the methods that are supposed to be operated by a boolean switch this issue doesn't occur but the values shouldn't allow a switching of the background to happen.
DukeFutzwillus DukeFutzwillus

2016/11/27

#
Sorry I meant when I check the values they say they are false and if that is true the if block shouldnt happen at all.
DukeFutzwillus DukeFutzwillus

2016/11/27

#
I am so confused by this because it was working and I am saying that these values should be false all over the place but it still changes.
danpost danpost

2016/11/27

#
Apparently you have 'static' fields in other classes that will need to be initialized in the MyWorld class as well (to mention one, PortaltoPlanet.nextLevel).
You need to login to post a reply.