Graphics Programs Reference
In-Depth Information
The inverse trig functions available in Flash are
Math.asin(z)
-1 <= z < = 1
Math.acos(z)
-1 <= z < = 1
Math.atan(z)
z can be any number
Math.atan2(y,x)
x and y can be any numbers
Exercise 5.5: Using the atan2 Function
The atan2 function is often used in situations such as aiming and shooting at targets in
games. It is also used for tracking the location of the cursor, as we will see in this exer-
cise. For reference, open 5_5_froggie.swf in the Chapter 5 folder. What we have, as
shown in Figure 5.30, is a flying bug whose position will be determined by the location
of the cursor. The frog's eyes continuously look at the bug wherever it goes. If the bug
gets too close to the frog's mouth, it will flick its tongue and eat the bug. Breaking this
down, we have three tasks in ActionScript: move the bug, rotate the eyes, and test the
bug for closeness to the tongue.
Figure 5.30 Using the atan2 function
Step 1: Getting started
Open 5_5_atan2.fla in the Chapter 5 folder. Since we want the bug to follow the
cursor without the cursor showing, we'll begin by hiding the cursor. Also, at this point,
the frog has not yet flicked its tongue, so we will set the variable flicked to false.
Search WWH ::




Custom Search