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
IsVarious IsVarious

2012/4/10

#
I've been through this forum, and I've yet to find a piece of code to simply do what I'm wanting. First off, I've added some items to the world with : public void LoadStartMenu(){ //Loads the Startup Menu addObject(new Startup(), 285, 285); //Loads the Color choice buttons (White or Black) addObject(new swp(), 412,255); addObject(new sbp(), 412,321); } And this works fine, but the issue I'm now having is after the user selects one of the objects on the screen, it doesn't remove all the objects I'm wanting. (only the object the user clicked on) I've tried using: "getWorld().removeObjects(Startup);" where 'startup' is the name of the object/class I'm trying to remove from the world. I've tried many variations of this as well, and all bring about the wrong result. And I keep getting a compile error. Also I'm trying to do this from within a subclass actor when it's clicked on. (e.g a button is created on the screen, you click on that button and only that button is removed, not the other two buttons that were loaded) So I'm open to any examples or ideas as to what I'm doing wrong or simply not understanding about this "getWorld().removeObjects(Startup);"
RedPhoneBooth RedPhoneBooth

2012/4/10

#
You could try making the swp(), sbp(), and startup() objects within the class, and remove each one systematically from the world when the user clicks on one. By removing the Startup class, that doesnt mean it removes the swp and sbp classes. I hope this helps :)
danpost danpost

2012/4/10

#
'StartUp' is a class, but the 'removeObjects' method takes a List of objects (not a class). You can create a List of objects of one class with 'getWorld().getObjects(Class cls)'. When you encounter problems as such, always check the API to make sure you are using the correct parameters. Although the error message you were getting should tell you as much (got Class, but expected List). Since you will be executing code in the Button class when the user selects a color, you will want to remove the StartUp object first; then remove the Button objects
getWorld().removeObjects(getWorld().getObjects(StartUp.class);
getWorld().removeObjects(getWorld().getObjects(Button.class);
The order is very important as, if you remove the buttons first, 'getWorld()' will be 'null' and fail when you try to remove the StartUp object.
Busch2207 Busch2207

2012/4/10

#
or u write:
World W = getWorld();
W.removeObjects(W.getObjects(StartUp.class);  
W.removeObjects(W.getObjects(Button.class); 
Then the order is vayne.
IsVarious IsVarious

2012/4/10

#
Thanks everyone for such fast feedback, but sadly I think I'm not being clear on what I'm attempting to convey. Although I'm glad you pointed out that the "getWorld().removeObjects(Startup)" is asking for a list. I've also tried "getWorld().removeObject(Startup)" I think it's important to point out that all I'm trying to do is remove other images/objects from the screen when the user clicks on one. So for example in my first piece of code, I load 3 images, (the names don't matter as they could be named image1, image2, image3, but so I could understand them as intended they have the names given.) I'm not sure what I'm doing wrong here, because I have tried every suggestion given here, and throughout the forum. Is it simply not possible to remove other objects on the screen, as a result of clicking on a different object ?
Builderboy2005 Builderboy2005

2012/4/10

#
It is completely possible. First let me ask, what is creating the actors and adding them into the world? Is the world doing this or is an Actor doing it?
IsVarious IsVarious

2012/4/10

#
I believe the world is. See the project I'm working on is the board game Reversi, and it loads the objects from the board class. Here is the method for that.... public void LoadStartMenu() { //Loads the Startup Menu addObject(new Startup(), 285, 285); //Loads the Color choice buttons (White or Black) addObject(new swp(), 412,255); addObject(new sbp(), 412,321); }
Builderboy2005 Builderboy2005

2012/4/10

#
Ah ok, so I have a question first. There is a simple way to remove all initilized objects of the same class, but it only works if you remove *all* objects of the same class, you can't choose. It works like so:
removeObjects(getObjects(ClassName.class));
getObjects() returns a list of all the classes of type ClassName that are currently in the world, and removeObjects() removes them. This works if you want to remove all objects of a certain class from the world. Otherwise, you will need to keep track of the objects you have added in order to remove them later.
Startup myStartup = new Startup();
Swp mySwp = new Swp();
Sbp mySbp = new Sbp();

public void LoadStartMenu(){
    addObject(myStartup, ....
    addObject(mySwp, ....
    addObject(mySbp, ....
}

public void RemoveStartMenu(){
    removeObject(myStartup);
    removeObject(mySwp);
    removeObject(mySbp);
}
IsVarious IsVarious

2012/4/10

#
I'll try this and get back to you shortly. Thanks a ton for this help btw.
IsVarious IsVarious

2012/4/10

#
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)
DHoffman94 DHoffman94

2012/4/10

#
Shouldn't it just be
getWorld.removeObject(this)
or
getWorld.removeObject(Blah.class)
?
IsVarious IsVarious

2012/4/10

#
DHoffman94 wrote...
Shouldn't it just be
getWorld.removeObject(this)
or
getWorld.removeObject(Blah.class)
?
I've tried both, and neither works.
IsVarious IsVarious

2012/4/10

#
For some reason, now I'm having an issue trying to run a method from within a class. For example, the method is in the World class, and I'm trying to use that method inside a subclass. Any ides why I get this error? Cannot find symbol - method .BCleanUpStartMenu()
danpost danpost

2012/4/10

#
The first part, 'getWorld.removeObject(this)', is OK as 'removeObject takes an Actor object as the parameter; however, Blah.class is a 'class', not an Actor object; therefore, 'getWorld.removeObject(Blah.class)' will not work.
IsVarious IsVarious

2012/4/10

#
danpost wrote...
The first part, 'getWorld.removeObject(this)', is OK as 'removeObject takes an Actor object as the parameter; however, Blah.class is a 'class', not an Actor object; therefore, 'getWorld.removeObject(Blah.class)' will not work.
What must I do to make it work ? In this method I add 3 objects, and I'm in turn wanting to remove those same objects after having clicked on one of the two buttons. (swp or sbp). public void LoadStartMenu() { //Loads the Startup Menu addObject(new Startup(), 285, 285); //Loads the Color choice buttons (White or Black) addObject(new swp(), 412,255); addObject(new sbp(), 412,321); } What would work instead? Thanks again for all your help guys.
There are more replies on the next page.
1
2
3