Graphics Programs Reference
In-Depth Information
Figure 5.33 Setting the registration point
Step 4: Check distance from the tongue
The next thing we need to do is check to see whether the bug is close enough to the
tongue to eat. We begin by setting the tongue's alpha to 0 if it has been flicked so that
we no longer see it (line 50). Next we calculate the distance between the bug and the
tongue using the equation for the distance between two points (lines 54-56).
If the distance is less than the height of the tongue and we haven't yet flicked, then the
frog is ready to eat (line 59). Note that the registration point for the tongue is at the
bottom of the tongue so that we can test for distance and rotate the tongue correctly.
47
48
49
50
51
52
53
54
55
56
57
58
59
60
function tongueTest()
{
// if the tongue has been flicked, then hide it
if ( flicked == true) { tongue_mc._alpha = 0;} ;
else
{
// calculate the distance between the bug and tongue
dxT = _xmouse - tongue_mc._x;
dyT = _ymouse - tongue_mc._y;
dist = Math.sqrt( dxT * dxT + dyT * dyT );
// if the bug is too close
if ( dist < tongue_mc._height && flicked == false)
{
When the bug is close enough to eat, there are several things we must do. We calculate
the angle between the bug and the tongue in radians, again using the atan2 function in
line 62. After converting the angle to degrees, we set the rotation of the tongue to that
angle. As before, we must add 90 degrees in line 63 because of the placement of the
registration point in the lower center, in this case a necessity. We show the tongue by
setting its alpha to 100 (line 64). The bug is eaten when its alpha is set to 0 (line 65).
We then restore the arrow cursor and set flicked to true (lines 66 and 67).
 
Search WWH ::




Custom Search