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

2019/1/13

access rotation of another class

dreyvel dreyvel

2019/1/13

#
I've got two sub classes of the class actor and I want to access the rotation of one class from another. how is that possible?
danpost danpost

2019/1/13

#
dreyvel wrote...
I've got two sub classes of the class actor and I want to access the rotation of one class from another. how is that possible?
Classes do not have a rotation -- but, Actor objects created from a class (a.k.a. instances of a class) do. For one actor to "find" another one, they both would need to be in the same world. The getWorld Actor class method returns the World object an actor is in. The getObjects World class method returns a List object containing all instances of the given class that are in a world. The list should not be empty if an actor of the given class was in the world and the get List class method returns an element from the list. The object returned from the list may need to be cast as an Actor object before the getRotation Actor class method can be called on the extracted element. Let us say you had an Actor1 and an Actor2 class, both subclasses of Actor. Then, in code:
1
int actor2rotation = ((Actor)getWorld().getObjects(Actor2.class).get(0)).getRotation();
There would probably be a simpler way if there was some interaction between the two actors (like touching).
data17 data17

2019/1/13

#
Good explanation! I like the concept & idea with the world and actors.
You need to login to post a reply.