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

2014/1/17

Removing objects

Draymothisk Draymothisk

2014/1/17

#
I've been stuck on this problem for a few days now. I have a few forloops adding rows of trees, and when stairs are hit, the trees need to be removed. I finally got the code to work without throwing an error, but I encountered a new problem. Successfully gets removed:
addObject(Tree, 200, 300);
Doesn't: addObject(new Tree(areaCount), 200, 300); This is a problem because the trees are added in in bulk, and with specific numbers, so I create many instances of them. I can't just have one tree. Any ideas on how to remove the trees with a specific number? It throws a Null Pointer with the second code pretty much saying it can't be removed because "Tree" isn't in the world (which it is), but works with the first because it "is" in the world.
Draymothisk Draymothisk

2014/1/17

#
And just a side note, but I don't want ALL the trees removed, so removing all instances of the class from the world isn't a solution, just the select few that meet the parameter in the second code.
davmac davmac

2014/1/17

#
What you've posted above is confusing:
    addObject(Tree, 200, 300);  
Do you have a variable called 'Tree'? Or did you mean:
    addObject(new Tree(), 200, 300);  
? Also:
It throws a Null Pointer with the second code pretty much saying it can't be removed because "Tree" isn't in the world (which it is)
Maybe you need to be a bit more specific here. Exactly where do you get the NullPointerException (and post the relevant code!)?
danpost danpost

2014/1/17

#
Normally what I would do (and this is presuming that the stairs are actors), is make use of the 'addedToWorld' method to make adjustments in location or existence of actors added to the world. This should have each tree check for and remove itself if stairs intersect it.
Draymothisk Draymothisk

2014/1/17

#
Basically, I spawn a bunch of trees in an area. "Tree" is an actor, and when it spawns, it is assigned a specific number. When my main actor hits stairs (another actor), I want to tell the Tree class to delete itself if it has the specific number I call. In my world, I create many trees using:
addObject(new Tree(), 200, 300);   
This throws a null pointer back to my stairs actor's act method (where the stairs tell the world to delete the trees). I can only delete ONE tree successfully if I use the code (below), but I need to delete the multitude:
addObject(Tree, 200, 300); 
@Danpost: The stairs don't intersect the Trees
Super_Hippo Super_Hippo

2014/1/17

#
Do you have the 'specific number' stored as a variable in the Tree class? Then you can try something like this:
for (Object obj : getWorld().getObjects(Tree.class))
{
    Tree tree = (Tree) obj;
    if (tree.x == y) //x is your specific number and the y is the one which it is compared
    {
        getWorld().removeObject(tree);
    }
}
Draymothisk Draymothisk

2014/1/21

#
That worked! I was thinking about trying casting (that's casting right?), but didn't get around to it yet. Thanks Hippo
Super_Hippo Super_Hippo

2014/1/21

#
I think it is called 'foreach-loop'. I found this code like a week ago somewhere here in the discussions. I think that danpost posted it, but I am not sure about it. I searched it, because I also needed to access more than one object at a time in one of my scenarios and this is a very nice way.
JasonZhu JasonZhu

2014/1/21

#
Its read for each Object obj in getWorld().getObjects(Tree.class) then...
You need to login to post a reply.