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

2014/9/13

If (addedToWorld... ?

nivilom86 nivilom86

2014/9/13

#
Hello ! I have a little question : is it possible to write the following field :
        if (addedToWorld(World world))
I try to do it, but it doesn't work. What I do wrong ? Thanks for reading ;)
danpost danpost

2014/9/13

#
The actor class automatically gets an 'addedToWorld' method that is, by default, empty. It is always called when an object of that class is added into a world. You could call it yourself in your code, however, its return type is 'void', not boolean (it provides a time to do those one-time actions you could not do before the actor was placed in the world). If you are just trying to find out if the actor is in a world, you can do this:
if (getWorld() != null)
nivilom86 nivilom86

2014/9/13

#
Ok thanks ! Then, it's not possible to write the field ?
danpost danpost

2014/9/13

#
nivilom86 wrote...
Ok thanks ! Then, it's not possible to write the field ?
What exactly do you mean by 'write the field'? What condition(s) are you trying to check for?
nivilom86 nivilom86

2014/9/13

#
I just try to know if the line I have written on the first can run.
danpost danpost

2014/9/13

#
As I said previously, 'addedToWorld' is a method that has no return value. So
if (addedToWorld(world)) // assuming the variable 'world' is assigned a World object
// is equivalent to
addedToWorld(world);
if () // this obviously will fail
nivilom86 nivilom86

2014/9/13

#
Ah, yes, obviously ^^
nivilom86 nivilom86

2014/9/13

#
Thanks !
danpost danpost

2014/9/13

#
If you want to process some code at the time the actor is added into a world, override the 'addedToWorld' method. Add the following into the Actor subclass:
protected void addedToWorld(World world)
{
    // action to take when actor is added into world
}
It will be called automatically when the World class method 'addObject' is called to add an Actor of the class into the world.
nivilom86 nivilom86

2014/9/14

#
OK thanks !
You need to login to post a reply.