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

2019/2/19

Why is object reference to World needed?

dejong1968 dejong1968

2019/2/19

#
When I start a new scenario, add classes and put objects of that classes in the world, every object automatically gets an object reference to the World. At least that's what I see when I rightclick the object and click 'inspect': I see a 'World world' reference there. If so, then why do I need to add another object reference to the world when needing something like adding a new object to the world. For example, a Gardener has to add a plant: if (Greenfoot.isKeyDown("z")) { World world = getWorld(); world.addObject(new Plant(), getX(), getY()); } That second line is needed, and I don't understand why, since there is already an objectreference to the world. When deleting the second line, the error says: "world is not public in greenfoot.Actor; cannot be accessed from outside package" Thanks in advance!
danpost danpost

2019/2/19

#
If you remove the second line, the compiler will look for a variable named 'world' in the Gardener class first; then in the Actor class (presuming the Gardener class extends the Actor class directly). It will not find one in the Gardener class, and when it looks in the Actor class, it will find one with private access (hidden to the Gardener class). 'private' means that direct access to the field can only be achieved within the class it it located in. A Gardener object must use the 'getWorld' method, which has public access, to acquire the World object reference. You could still get away with removing line 2, if you use this:
1
getWorld().addObject(new Plant(), getX(), getY());
dejong1968 dejong1968

2019/2/19

#
Thanks for your quick answer. I understand the error message now, but I still don't understand why I have to use the World world when that reference is already defined for that object.
danpost danpost

2019/2/19

#
dejong1968 wrote...
Thanks for your quick answer. I understand the error message now, but I still don't understand why I have to use the World world when that reference is already defined for that object.
What exactly do you not understand? Are you expecting that you could just write:
1
addObject(new Plant(), getX(), getY());
in the Gardener class? Or, what do you expect you could do?
dejong1968 dejong1968

2019/3/25

#
Yes, that is what I expect because when I add an Object to the world, that object automatically gets an object reference to the World. At least that's what I see when I rightclick the object and click 'inspect': I see a 'World world' reference there. So the line
1
world.addObject(new Plant(), getX(), getY());
shoud work without the line
1
World world = getWorld();
... because the object reference is already there when I add the object to the world.
Super_Hippo Super_Hippo

2019/3/25

#
As danpost already wrote above, the "world" variable is private. So in the Actor class, it looks like this (probably, didn't check):
1
2
3
4
5
6
private World world;
 
public World getWorld()
{
    return world;
}
Since your code is not in the Actor class but only in one of its subclasses, you can not access the "world" variable directly because it is private, but you can use the "getWorld" method because it is public.
danpost danpost

2019/3/25

#
Super_Hippo wrote...
As danpost already wrote above, the "world" variable is private. So in the Actor class, it looks like this (probably, didn't check): << Code Omitted >> Since your code is not in the Actor class but only in one of its subclasses, you can not access the "world" variable directly because it is private, but you can use the "getWorld" method because it is public.
Actually, I was mistaken -- the field is not private. However, it is not declared public and therefore it is not directly accessible to any class not included in the greenfoot package. You must use the public method to acquire its value.
dejong1968 dejong1968

2019/3/26

#
Clear now, thanks!!
You need to login to post a reply.