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

2017/6/24

Help me Please

lorena1999 lorena1999

2017/6/24

#
I recently started a new Greenfoot program ... I don't know what the problem is, the terminal window keeps appearing ... i have a class named "Rac" and another one "Om" and i want to get the coordinate x of the class "Om" and the coordinate x of the class "Rac", now i want to do that in the "Rac"'s class because i want if OmX > RacX , the object from "Rac" to turn its direction with 180 degrees and i don't know how to acces the class "Om" in the class "Rac" ... they are both Actors and it keeps appearing that terminal Window...
Super_Hippo Super_Hippo

2017/6/24

#
You need a reference to the Om object. If there is always one in the world, you can do this:
int x = getWorld().getObjects(Om.class).get(0).getX();
danpost danpost

2017/6/24

#
First thing is that it is not the class that has coordinates -- it is the objects of the class (that are subclasses of Actor) that have coordinates. That being said, you need to acquire a reference to the Om object in your world from within the Rac class. If there is always an Om object in the world, you can use:
Actor om = (Actor)getWorld().getObjects(Om.class).get(0);
to acquire that reference (in 'om'). However, if it may be possible that there is no Om object in the world, you will need to preface that with the following:
if ( ! getWorld().getObjects(Om.class).isEmpty() )
With the reference, you can use 'om.getX()' and 'om.getY()' to get the Om object's coordinates.
You need to login to post a reply.