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

2020/1/19

getWorld().removeObject();

NotAProYet NotAProYet

2020/1/19

#
Hello, I am having some issues with the getWorld().removeObject(); comment. I would like to remove my WinScreen for the first Level so that I can start a new Level whenever the next Level button is pressed. That is my code in the nextLVL.class:
Archerfish2 Archerfish2

2020/1/19

#
Hey NotAProYet! I believe the issue is that the "remove next level button" occurs first. Thus, the "remove winScreen" line does not get executed.
danpost danpost

2020/1/19

#
NotAProYet wrote...
public void nextLVL(){
    if(Greenfoot.mouseClicked(this)){
        getWorld().removeObject(this);
        getWorld().removeObject(WinScreenLVL1.class);
            
        
    }
    
}
Switch lines 3 and 4. Once you remove this (your line 3), getWorld (on your line 4) will not return a world (it will return a null value).
NotAProYet NotAProYet

2020/1/20

#
Okay I switched lines 3 and 4 and then removed the this should I do something like if(returns null) ? my code looks like that now:
public void nextLVL(){
        if(Greenfoot.mouseClicked(this)){            
            getWorld().removeObject(WinScreenLVL1.class);
            getWorld().removeObject();
        
        }
    
    }
could you please write the next steps again? And thanks for your help already!
danpost danpost

2020/1/20

#
NotAProYet wrote...
Okay I << blah, blah, blah >> and then removed the this
Why remove 'this'?
NotAProYet NotAProYet

2020/1/20

#
Because you wrote
danpost wrote...
Once you remove this ....
danpost danpost

2020/1/20

#
NotAProYet wrote...
Because you wrote
danpost wrote...
Once you remove this ....
I was trying to explain what happens (why it won't work) when you remove 'this'. 'this' is referring to the actor that is currently acting. To clarify, once that actor ('this') is removed from the world, you will not get your world object with the getWorld method anymore. So, in the problem code you provided above, your line 4 will cause a NullPointerException error because line 3 took that actor out of the world.
NotAProYet NotAProYet

2020/1/20

#
Okay, that´s great! Now I get where the problem was but it still isn't working. It says something like "incompatible types: java.lang.Class<WinScreenLVL1> cannot be converted to greenfoot.Actor" I think I have to do something like "Actor actor = new WinScreenLVL1.class" but I still have not found out what exactly to do.
danpost danpost

2020/1/20

#
NotAProYet wrote...
Okay, that´s great! Now I get where the problem was but it still isn't working. It says something like "incompatible types: java.lang.Class<WinScreenLVL1> cannot be converted to greenfoot.Actor" I think I have to do something like "Actor actor = new WinScreenLVL1.class" but I still have not found out what exactly to do.
You can use World instance method getObjects (with removeObjects):
getWorld().removeObjects(getWorld().getObjects(WinScreenLVL1.class));
NotAProYet NotAProYet

2020/1/20

#
Many many thanks! It is working just perfectly!
You need to login to post a reply.