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

2013/2/21

Chapter 6, page 89, Greenfoot book, international edition

allekalle allekalle

2013/2/21

#
Hi! Can somebody please explain this to me? Under exercise 6.18 at the bottom of page 89, it says "...so it must be called on a World object." On the next page it shows: "getWorld().getObjects(Body.class)" to be used to "get objects". Why can´t dot notation work? Like in Greenfoot.playsound("3a.wav""); for example. Can somebody please expand on this? What is the idea? Thanks! Karl
Jonas234 Jonas234

2013/2/21

#
I am not really sure if i understand your question correctly, but: playSound is a static method, so you can call it by writing the class in front of the dot, so you can write: Greenfoot.playSound(); getObjects() needs an Object to work, so you need something like: object.getObjects(); To get this object they are just executing the method getWorld() which returns the object of the current World. After this you do the same as above: getWorld().getObjects(); Jonas
allekalle allekalle

2013/2/22

#
OK, thanks for the answer. I will read it again in the afternoon and hopefully understand it better. I am a bit lost among the dot notations... Perhaps: Instead of using the name of the object, like if the name of the object was object1, having a method1(), and one could make the method call: "object1.method1();", but if one doesn´t know the name of the (World) object, one can use the getWorld() method to access a method of this object? Thanks! Karl PS: Where is the getWorld() method defined?
danpost danpost

2013/2/22

#
The 'getWorld' method is defined in the Actor class API and will return the world that the object is in (or 'null' if the object in not in any world). The returned value is only cast to be a World object and will only execute methods that are in the World class API. If there is only one sub-class of world in your scenario that the object can be in, then you can cast it directly to that type and execute methods and access fields within that class.
// example
World world = getWorld();
MyWorld myWorld = (MyWorld) world;
Counter counter = myWorld.getCounter();
// or in a one-liner
Counter counter = ((MyWorld)getWorld()).getCounter();
If your actor could be in one of several worlds, then your code will have to cover each possibility individually.
World world = getWorld();
Counter counter = null;
if (world instanceof MyWorld) counter = ((MyWorld)getWorld()).getCounter();
if (world instanceof HisWorld) counter = ((HisWorld)getWorld()).getCounter();
if (world instanceof HerWorld) counter = ((HerWorld)getWorld()).getCounter();
davmac davmac

2013/2/22

#
Perhaps: Instead of using the name of the object, like if the name of the object was object1, having a method1(), and one could make the method call: "object1.method1();", but if one doesn´t know the name of the (World) object, one can use the getWorld() method to access a method of this object?
I think you're not understanding the difference between classes and objects exactly. A world class is not an actual world - it just defines a world behavior. There could be more than one world of that class in existence (though Greenfoot will only display one world at a time). There are two types of methods: static methods (class methods) and non-static methods (or "instance methods" or "object methods"). The playSound() method is a static method in the Greenfoot class. So you call it as: Greenfoot.playSound("abc.wav"); The getObejcts() method is an instance method in the World class. You can't call: World.getObjects(Body.class) ... because you haven't specified which world you want to get the objects of. Suppose you have a world class called MyWorld, you might try: MyWorld.getObejcts(Body.class) But this still doesn't work! - because there could be more than one instance of MyWorld, and you haven't specified which one you want to call the method on. The getWorld() method (an instance method in Actor) returns the actual world, and so you can call getObjects() on what it returns: getWorld().getObjects(Body.class) ... is fine.
allekalle allekalle

2013/2/23

#
Ok, I think I'm starting to understand it. Thanks for the answers!
allekalle allekalle

2013/3/3

#
By the way, sorry for my fuzzy question. I hope I will be less confused as I progress here. I suppose I wanted to ask: if one knows the name of the World object which the actor is in; let´s say the world object is called "world1", isn't it allowed to write: world1.getObjects(Body.class);? Thanks! Karl
davmac davmac

2013/3/3

#
I suppose I wanted to ask: if one knows the name of the World object which the actor is in; let´s say the world object is called "world1", isn't it allowed to write: world1.getObjects(Body.class);?
Well, objects don't really have names. Only variables (which can refer to objects) have names. Variables have a scope, meaning that they belong in a particular method or a particular class and can only be accessed from within that method/class. But yes, if you have a variable called world1 which olds a reference to the world, then you could write "world1.getObjects(Body.class)".
allekalle allekalle

2013/3/3

#
Ok! I can't say it's crystal clear yet, but I'm getting there. :-) In the example in the book on pages 89 onwards, the world object is not defined (or referred to by a variable) anywhere in the body class, which means it is necessary to use a method that returns the variable (in a List in this case) referring to the object (?). Thanks!
You need to login to post a reply.