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

2012/6/4

removedFromWorld

1
2
USBest USBest

2012/6/4

#
Is there a method like 'addedtoWorld(World W)' just for removing an object?
kiarocks kiarocks

2012/6/4

#
No, there isn't.
USBest USBest

2012/6/4

#
And how can I let something happen if an object is removed?
USBest USBest

2012/6/4

#
Can't anyone help me?
davmac davmac

2012/6/4

#
You can just add additional logic in the code which removes the object:
1
2
world.removeObject(actor);
// do other stuff here...
SPower SPower

2012/6/6

#
You can overwrite removeObject doing this:
1
2
3
4
5
public void removeObject(YourActor object)
{
     super.removeObject(object);
     object.removedFromWorld(this);
}
Add that method to MyActor, and you're done! Note that this will only work on MyActor objects or subclasses of it.
USBest USBest

2012/6/6

#
I get the message: 'cannot find symbol - class YourActor' I changed it to MyActor and the I got the message: 'cannot find symbol - method removeObject(MyActor)'
SPower SPower

2012/6/6

#
You must replace the class YourActor by the class you want removedFromWorld to be invoked on, of course!
USBest USBest

2012/6/6

#
I still get the warning: 'cannot find symbol - method removeObject(Menu)'
SPower SPower

2012/6/6

#
The place where you call it, you have to cast the getWorld to your own subclass of world, like this:
1
2
MyWorld w = (MyWorld) getWorld();
w.removeObject(Menu);
where you have to replace MyWorld with your own world. Does this help?
USBest USBest

2012/6/6

#
Well My code is now:
1
2
3
4
5
6
public void removeObject(Menu object)
{
     world world = (world)getWorld();
     world.removeObject(object);
     object.removedFromWorld(this);
}
I get no error, but it don't work...
SPower SPower

2012/6/6

#
??? You have to overwrite the method removeObject in your subclass of world, like this:
1
2
3
4
5
public void removeObject(YourActor object) 
    super.removeObject(object); 
    object.removedFromWorld(this); 
}
With my previous post, I thought you were programming in your Actor class :) It should work fine now, if you ignore my previous post :)
SPower SPower

2012/6/6

#
If you still get an error, this might work:
1
super.removeObject((Actor) object);
in the removeObject method in your own world.
USBest USBest

2012/6/6

#
I did. Now I've got in my world class:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public class world  extends World
{  
    private int MouseX=0,MouseY=0;
    private static int MouseScroll=0;
 
    public world()
    {   
        super(600, 600, 1);
        Objects(new Beam());
        CreateMenu();
    }
 
    public void removeObject(Menu object)
    
        super.removeObject(object); 
        object.removedFromWorld(this); 
    }
    [...]
}
But it still don't work, when I press the close Button:
1
2
3
4
5
public void act()
{
    if(Greenfoot.mouseClicked(this) && getWorld().getObjects(Menu.class).size()>0)
        getWorld().removeObject(getWorld().getObjects(Menu.class).get(0));
}
There is just removed the menu and not the buttons on it...
SPower SPower

2012/6/6

#
Can you show me the removedFromWorld method?
There are more replies on the next page.
1
2