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

2021/3/5

How to change world using inheritance?

Risen Risen

2021/3/5

#
I need the subclass to be able to address the class with the variable "world", which is one of the worlds
 //class
public void Changer(String world) {
        Greenfoot.setWorld(new MyWorld());
    }
//subclass
if(Greenfoot.mouseClicked(this)) {
                 lcb.Changer("MyWorld");
                }
The problem is that I don't know what type of variable to put for world
danpost danpost

2021/3/5

#
Line 7 should just be:
Changer("MyWord");
Risen Risen

2021/3/6

#
Thank you, but how can i fix this error? java.lang.NullPointerException: The given world cannot be null.
//class
public void Changer(World world) {
        Greenfoot.setWorld(world);
    }
//subclass
public class Back extends LevelChooseButtons
{
    MyWorld mw;
    public void act() 
    {
        if(Greenfoot.mouseClicked(this)) {
                 Changer(mw);
                }
    }       
    } 
danpost danpost

2021/3/6

#
Line 8 needs to be assigned a world.
Risen Risen

2021/3/6

#
danpost wrote...
Line 8 needs to be assigned a world.
Yeah, it works. Thank you very much
danpost danpost

2021/3/6

#
Actually, I do not see the purpose of the Changer method. It basically duplicates Greenfoot.setWorld. Why add the extra level of complexity when you just go directly to Greenfoot.
Risen Risen

2021/3/6

#
danpost wrote...
Actually, I do not see the purpose of the Changer method. It basically duplicates Greenfoot.setWorld. Why add the extra level of complexity when you just go directly to Greenfoot.
Changer is in a class that has multiple subclasses that go to other worlds. I did it for convenience.
danpost danpost

2021/3/6

#
Risen wrote...
Changer is in a class that has multiple subclasses that go to other worlds. I did it for convenience.
Isn't using Greenfoot.setWorld convenient enough?
Risen Risen

2021/3/6

#
danpost wrote...
Risen wrote...
Changer is in a class that has multiple subclasses that go to other worlds. I did it for convenience.
Isn't using Greenfoot.setWorld convenient enough?
Everyone has their own representation about convenience.
Risen Risen

2021/3/6

#
danpost wrote...
Risen wrote...
Changer is in a class that has multiple subclasses that go to other worlds. I did it for convenience.
Isn't using Greenfoot.setWorld convenient enough?
Ok, I understand, you were right. My method wasn't right 'cause after creating 6
MyWorld mv = new MyWorld();
in 1 class, I went to "java.lang.outofmemoryerror: java heap space". Also, one more question: this is my first time working with GreenfootSound, so is it normal, that I hear background music from 2 different worlds at the same time?
rocket770 rocket770

2021/3/7

#
I really don't understand why you need a new method to change the world, it makes no difference and if anything, having an extra layer just makes in inconvenient if anything since it is causing problems? As for the sound, create a sound object where you can then pause or stop it before you exit the world.
GreenfootSound sound = new GreenfootSound("Filename");
Start the sound using sound.play(); then you may pause it or stop it using
sound.stop();
or
sound.pause();
You need to login to post a reply.