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

2012/4/10

How can I remove an object from the world?

1
2
3
Builderboy2005 Builderboy2005

2012/4/10

#
IsVarious wrote...
So I loaded your first, "removeObjects(getObjects(Pieces.class)); "choice into a method inside the board class, and then called that method from one of the buttons. Where Pieces is the superclass of the other buttons/menu items being added to the world. here's the error I received. java.lang.NullPointerException at sbp.CleanUpStartMenu(sbp.java:39) at sbp.CleanUpStartMenu(sbp.java:44) at sbp.act(sbp.java:29) at greenfoot.core.Simulation.actActor(Simulation.java:507) at greenfoot.core.Simulation.runOneLoop(Simulation.java:470) at greenfoot.core.Simulation.runContent(Simulation.java:204) at greenfoot.core.Simulation.run(Simulation.java:194)
Could you post the code for sbp? Specifically line 39?
danpost danpost

2012/4/10

#
Where is your BCleanUpStartMenu() method and what does it look like? also, what code do you have to setup and call that method?
danpost danpost

2012/4/10

#
Are all three classes, 'Startup', 'swp', and 'sbp', sub-classes of the Pieces.class?
IsVarious IsVarious

2012/4/10

#
Builderboy2005 wrote...
IsVarious wrote...
Could you post the code for sbp? Specifically line 39?
I think I figured out why it was having that error, I was calling the method "CleanUpStartMenu();" which happend to be in two locations inside my project. One inside the Board class, and the other in the sbp class. So the error was caused because the method was inside the sbp class, and it kept looping itself. Now I've removed one of the methods, and left the first intact still in the world class. My current issue is trying to call that method from within my subclass. I've yet to test the code from your last suggestion as it's inside the "CleanUpStartMenu();" method, and wasn't called yet. Any ideas?
IsVarious IsVarious

2012/4/10

#
danpost wrote...
Are all three classes, 'Startup', 'swp', and 'sbp', sub-classes of the Pieces.class?
Yes they are.
IsVarious IsVarious

2012/4/10

#
danpost wrote...
Where is your BCleanUpStartMenu() method and what does it look like? also, what code do you have to setup and call that method?
As I posted above, I was having issues with two methods named the same, and since then I've removed the one, and now renamed the "BCleanUpStartMenu();" method to "CleanUpStartMenu();" it now looks like this... public void CleanUpStartMenu() { removeObject(myStartup); removeObject(myswp); removeObject(mysbp); } This method is inside the worldclass, and I'm attempting to call it from a subclass of the Pieces class. This is what I used to attempt to call it there.. CleanUpStartMenu(); and from that I recieved the error... Cannot find symbol - method .BCleanUpStartMenu()
danpost danpost

2012/4/10

#
Then from within the 'swp' class, as well as within the 'sbp' class, you can remove All Pieces.class objects with
getWorld().removeObjects(getWorld().getObjects(Pieces.class));
IsVarious IsVarious

2012/4/10

#
Builderboy2005 wrote...
IsVarious wrote...
Could you post the code for sbp? Specifically line 39?
The only thing inside the sbp class is inside the act method. public void act() { if (Greenfoot.mouseClicked(this)){ // Sets the current playing color pColor = 2; // Adds the GameInProgress Menu getWorld().addObject(new GameInProgress(), 630, 365); // Removes the StartupMenu and connected buttons CleanUpStartMenu(); } }
danpost danpost

2012/4/10

#
If the name of your sub-class of the World is 'ReversiWorld' then in the 'swp' class or the 'sbp' class, you can use
((Reversi) getWorld()).CleanUpStartMenu();
which gets the 'Reversi' world, that has your method in it, and calls the method.
danpost danpost

2012/4/10

#
where is pColor declared? and what is the name you gave for the sub-class of World?
IsVarious IsVarious

2012/4/10

#
I'll try these suggestions, and get back to you shortly. Thanks again.
IsVarious IsVarious

2012/4/10

#
danpost wrote...
where is pColor declared? and what is the name you gave for the sub-class of World?
pColor is declared in the Board class. Ok as an update so people understand the tree of classes, it's setup like this. World - Board.class Actor - Pieces.class - Black.class - sbp.class Actor - Pieces.class - White.class - swp.class I have other classes, but I'm not dealing with them at this moment so there's no need to include them.
danpost danpost

2012/4/10

#
danpost wrote...
If the name of your sub-class of the World is 'ReversiWorld'
Sorry, that was supposed to be 'Reversi' (not 'ReversiWorld').
IsVarious IsVarious

2012/4/10

#
danpost wrote...
If the name of your sub-class of the World is 'ReversiWorld' then in the 'swp' class or the 'sbp' class, you can use
((Reversi) getWorld()).CleanUpStartMenu();
which gets the 'Reversi' world, that has your method in it, and calls the method.
This suggestion worked perfectly, and in conjunction with an option given by Builderboy2005 it seems that it does exactly what I've been wanting. Thanks a ton guys. Any idea as to why it wasn't working before?
danpost danpost

2012/4/10

#
IsVarious wrote...
public void act() 
{    
    if (Greenfoot.mouseClicked(this)){
       pColor = 2;
       getWorld().addObject(new GameInProgress(), 630, 365);
       CleanUpStartMenu();  
    }
}
OK, so instead of the above, try
public void act()
{
    if (Greenfoot.mouseClicked(this))
    {
        Board board = (Board) getWorld();
        board.pColor = 2;
        board.addObject(new GameInProgress(), 630, 365);
        board.CleanUpStartMenu();
    }
}
There are more replies on the next page.
1
2
3