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

2013/4/21

Accessing methods in other objects??

1
2
allekalle allekalle

2013/4/26

#
Well, for instance to change the settings of all the objects that have been created, or to get some settings of some fields of them. I suppose the getOneIntersectingObject method somehow has to find the active objects first, then calculate where they all are, find one that intersects, and then pass a reference to that object. It would be interesting to know how to find the active objects (and get references to them), and to find out how that method does it. Cheers, Karl
danpost danpost

2013/4/26

#
I believe there is a way to download the source of the greenfoot package. Go to the 'Download' page; on the lower right, there is a link to the 'Source code' for the Greenfoot IDE.
davmac davmac

2013/4/26

#
I believe there is a way to download the source of the greenfoot package
We're both guessing, but I don't think that's really going to help. akkekalle, you can get all objects in the world by using the getObjects() method (from the World class), with a null parameter. It returns a list. For example: List allObjects = getWorld().getObjects(null); You would then generally use a 'for' loop to go through all the objects and do something with them:
    for (Object o : allObjects) {
        Actor a = (Actor) o;  // cast the object to Actor type
        ... // do what you want here
    }
To answer your question:
I suppose the getOneIntersectingObject method somehow has to find the active objects first, then calculate where they all are, find one that intersects, and then pass a reference to that object.
It certainly could work that way, but that would be inefficient and is not how it actually works. Greenfoot has a a sophisticated collision-checking algorithm which I won't explain in detail here, but basically it involves dividing the world into smaller and smaller areas so that you can discard most actors from the search immediately (without checking each one in turn to see if it intersects). If you were really interested in the details, you could download the source code as danpost suggests, but I suspect you would have trouble understanding it at this stage.
allekalle allekalle

2013/4/26

#
OK, thanks for bearing with me. I will try to continue coding. :-)
Friendly_Hacker Friendly_Hacker

2016/7/4

#
danpost wrote...
What do you mean by 'more "direct" access? What do you want to accomplish?
I think he means a simpler access.
You need to login to post a reply.
1
2