Java Reference
In-Depth Information
TRANS_NONE
TRANS_ROT90
TRANS_ROT180
TRANS_ROT270
TRANS_MIRROR
TRANS_MIRROR_ROT90
TRANS_MIRROR_ROT180
TRANS_MIRROR_ROT270
To make Dr. Quatsch face right instead of left, you would apply a TRANS_MIRROR transfor-
mation. To understand all the transformations, see the Sprite API documentation, which
contains a set of fighter plane images that are very helpful.
The only tricky part about transformations is the reference pixel . All Sprite s have a refer-
ence pixel, which is expressed in the Sprite 's own coordinate space; by default, the reference
pixel is located at 0, 0 , the upper-left corner of the Sprite . When the Sprite is transformed, the
reference pixel is also transformed.
When a transformation is applied, the Sprite 's position is changed so that the current
location of the reference pixel does not change, even after it is transformed. For example,
Figure 14-6 shows how the position of the Sprite changes when a simple TRANS_MIRROR trans-
formation is applied.
Figure 14-6. The reference pixel doesn't move.
Suppose, for example, that the original position of the Sprite was 100, 100 (in the coordinate
system of the container), and the reference pixel position was 0, 0 (in the coordinate system of
the Sprite ). After applying a TRANS_MIRROR rotation, the Sprite 's position is adjusted so that the
transformed reference pixel is in the same location as the original reference pixel. Because the
frame width is 48 pixels, the Sprite 's position (its upper-left corner) changes from 100, 100 to
52, 100 .
To adjust the location of the reference point in the Sprite 's untransformed coordinate
system, use this method:
public void defineReferencePixel(int x, int y)
In the case of Dr. Quatsch, we want to apply a mirror transformation without having the
Sprite move, so we set the reference pixel to be at the center of the 48×48 frame:
// Sprite quatsch is defined as before.
quatsch.defineReferencePixel(24, 24);
Search WWH ::




Custom Search