Game Development Reference
In-Depth Information
Whenever you need to rotate an object toward another object, this is formula you need to use. It's
probably about 50% of the trigonometry you'll ever need to know for your games, and you didn't
actually have to do the math yourself! In Chapter 10, you'll look at a few more of AS3.0's trigonometry
functions.
Rotating the frog's eyes toward the player object
Of course, you don't want to rotate the frog; you want to rotate its eyes, which are subobjects. You
need to put together the rotation code with the earlier code to convert an object's local coordinates
to global coordinates. Here's how:
1. Add an AJPAN[BN=IA event handler to the bnkc object in the constructor method:
lq^he_bqj_pekjI]ej[>qc?]p_dan\$%
w
]``ArajpHeopajan\$Arajp*AJPAN[BN=IA(kjAjpanBn]ia%7
bnkc*]``ArajpHeopajan\$Arajp*AJPAN[BN=IA(kjBnkcHkkg%7
y
2. Create the kjBnkcHkkg event handler to rotate the eyes:
lner]pabqj_pekjkjBnkcHkkg\$arajp6Arajp%6rke`
w
++?kjranplkejpobnkihk_]hpkchk^]h_kkn`ej]pao
++Necdpaua
r]nbnkcoNecdpAua6Lkejp9jasLkejp\$bnkc*necdpAua*t( ±
bnkc*necdpAua*u%7
r]nbnkcoNecdpAua[T6Jqi^an9bnkc*hk_]hPkChk^]h\$bnkcoNecdpAua%*t7
r]nbnkcoNecdpAua[U6Jqi^an9bnkc*hk_]hPkChk^]h\$bnkcoNecdpAua%*u7
++Habpaua
r]nbnkcoHabpAua6Lkejp9jasLkejp\$bnkc*habpAua*t(bnkc*habpAua*u%7
r]nbnkcoHabpAua[T6Jqi^an9bnkc*hk_]hPkChk^]h\$bnkcoHabpAua%*t7
r]nbnkcoHabpAua[U6Jqi^an9bnkc*hk_]hPkChk^]h\$bnkcoHabpAua%*u7
++Nkp]paauao
bnkc*necdpAua*nkp]pekj9I]pd*]p]j.\$bnkcoNecdpAua[U ±
)lh]uan*u(bnkcoNecdpAua[T)lh]uan*t%&\$-4,+I]pd*LE%7
bnkc*habpAua*nkp]pekj9I]pd*]p]j.\$bnkcoHabpAua[U ±
)lh]uan*u(bnkcoHabpAua[T)lh]uan*t%&\$-4,+I]pd*LE%7
y
3. Save the I]ej[>qc?]p_dan*]o file and test the project. The frog's eyes now follow the cat as it
jumps around the stage.
This is an incredibly common requirement in games. File this one away for safekeeping; you'll be
returning to it again and again.