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

2015/5/2

Keep object when moving to new world

1
2
3
Linkk0 Linkk0

2015/5/4

#
That is it up now cheers
danpost danpost

2015/5/4

#
Okay, I got it downloaded. You can now, if you want, login, go to your scenario and click on 'delete scenario'.
danpost danpost

2015/5/4

#
First thing you need to do is this; (1) right click on Main_World class; and, (2) select 'new Main_World()' (click on) Then try running it.
Linkk0 Linkk0

2015/5/4

#
yeah, sorry I had it on the A1 world just so I could see what was there. when I run it directly from the Main_world class, once it starts it moves to the A! world, but if I pick up the heart - increasing the health, then move to the next world it goes back to 2
danpost danpost

2015/5/4

#
I cleaned up the Main_World class and had it start the A1 world automatically (before you start the project) so you would not have the blank screen.
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
import greenfoot.*;
 
public class Main_World extends World
{
    static Player player;
    static Lives lives;
 
    public Main_World()
    {   
        super(960, 640, 1);
        player = new Player();
        lives = new Lives();
        startOver();
    }
 
    public Main_World(int w, int h, int c)
    {
        this(w, h, c, true);
    }
 
    public Main_World(int w, int h, int c, boolean b)
    {
        super(w, h, c, b);
        setFields();
    }
     
    public static void startOver()
    {
        Greenfoot.setWorld(new A1());
    }
 
    public void setFields(){}
    public void nextLevel(){}
    public void resetLevel(){}
}
The sub worlds (A1 and A2) will need their 'super' calls in the constructors.
Linkk0 Linkk0

2015/5/4

#
Should it just be super(960, 640, 1); because when I put that in i get an error, and without that in I got a lot of errors from copying the main class code in
danpost danpost

2015/5/4

#
The A1 class code should be like this:
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
import greenfoot.*;
 
public class A1 extends Main_World
{
    public A1()
    {
        super(960, 640, 1);
        prepare();
        addObject(Main_World.player, 120, 120);
        addObject(Main_World.lives, 89, 40);
    }
 
    public void nextLevel()
    {
        Greenfoot.setWorld(new A2());
    }
 
    private void prepare()
    {
        Enemy enemy = new Enemy();
        addObject(enemy, 406, 198);
        Earth_Boss earth_boss = new Earth_Boss();
        addObject(earth_boss, 575, 420);
        Mana mana = new Mana();
        addObject(mana, 106, 95);
        mana.setLocation(89, 65);
        Water_Artefact water_artefact = new Water_Artefact();
        addObject(water_artefact, 190, 375);
        Mana_Powerup mana_powerup = new Mana_Powerup();
        addObject(mana_powerup, 224, 195);
        removeObject(mana_powerup);
        Earth_Artefact earth_artefact = new Earth_Artefact();
        addObject(earth_artefact, 175, 449);
        Fire_Artefact fire_artefact = new Fire_Artefact();
        addObject(fire_artefact, 180, 548);
        Health health = new Health();
        addObject(health, 231, 308);
        Mana_Potion mana_potion = new Mana_Potion();
        addObject(mana_potion, 226, 244);
    }
}
Linkk0 Linkk0

2015/5/4

#
I'm getting an eror when I try to compile that says java.lang.NullPointerException at greenfoot.World.addObject(World.java:394) at A1.<init>(A1.java:9)
danpost danpost

2015/5/4

#
danpost wrote...
First thing you need to do is this; (1) right click on Main_World class; and, (2) select 'new Main_World()' (click on) Then try running it.
The Main_World world must execute first to create the Player and Lives objects.
Linkk0 Linkk0

2015/5/4

#
YES! That's it! Thank you soo much for the help, and sorry for taking up so much of your time!
You need to login to post a reply.
1
2
3