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

2014/4/7

Rotating Objects?

allyand17 allyand17

2014/4/7

#
I was wondering if there was a way I could make an object and attach it onto another object so I could make it rotate on the same axis as the other object. Or is there another way to do this?
danpost danpost

2014/4/7

#
Please describe what you mean by 'rotate on the same axis as the other object' in detail. Specify whether the objects will move as well as rotate and whether they rotate in sync with each other or not.
allyand17 allyand17

2014/4/8

#
I currently have one of the objects rotating but I want the other one to rotate in sync. I want the object to move and look like it isn't a separate object.
danpost danpost

2014/4/8

#
The best way to do this (provided the center of both images is the center of rotation) is to have the first object control the location and rotation of the second object. This can simply be done with (I will use ClassName for the class of the actor that needs syncing) and this would go in the class of the object that the other is being synced with:
1
2
3
4
5
6
7
8
9
public void syncObject()
{
    ClassName actor = getOneIntersectingObject(ClassName.class);
    if (actor != null)
    {
        actor.setLocation(getX(), getY());
        actor.setRotation(getRotation());
    }
}
Perform this action after any movement code of this actor (or call this method at the end of the 'act' method).
allyand17 allyand17

2014/4/8

#
Thanks!
You need to login to post a reply.