Using relative rotation
Rotating entities relative to the position of other entities in 2D space is a great function to know. The uses for relative rotation are limitless and always seems to be a "hot topic" for newer mobile game developers. One of the more prominent examples of this technique being used is in tower-defense games, which allows a tower's turret to aim towards the direction that an enemy, non-playable character is walking. In this recipe, we're going to introduce a method of rotating our Entity
objects in order to point them in the direction of a given x/y position. The following image displays how we will create an arrow on the scene, which will automatically point to the position of the circle image, wherever it moves to:
Getting ready…
We'll need to include two images for this recipe; one named marble.png
at 32 x 32 pixels in dimension and another named arrow.png
at 31 pixels wide by 59 pixels high. The marble can be any image. We will simply drag this image around the scene...