Game Development Reference
In-Depth Information
The move_to_contact_with Script
1: {
2: var dirn, max_dist, contact_obj, dx, dy;
3:
4: dirn = argument0;
5: max_dist = argument1;
6: contact_obj = argument2;
7:
8: if( max_dist == -1 ) then max_dist = 1000;
9:
10: dx = lengthdir_x( 1, dirn );
11: dy = lengthdir_y( 1, dirn );
12:
13: dist = 1;
14:
15: while( dist <= max_dist )
16: {
17: if(place_meeting(x+dx,y+dy,contact_obj) == true) then return true;
18: x = x + dx;
19: y = y + dy;
20: dist = dist + 1;
21: }
22:
23: return false;
24: }
So just like before, the script begins in lines 2-6 by setting up temporary variables to hold
the a r g ume n t v a l ue s. Doi n g thi s i s by n o me a n s compul sor y , a s y ou coul d j ust use argument0 and
so forth directly in the code. However, it does improve the readability of the rest of the script
because numbered arguments are pretty meaningless.
Line 8 checks to see if the distance argument has been set to -1 , as this is the value used in
the original Move to Contact action to signify an “arbitrary” moving distance. That arbitrary
distance turns out to be 1000 pixels, so we're doing the same.
Lines 10 and 11 use the lengthdir_x and lengthdir_y functions to convert a distance of 1
pixel in the angle dirn into its component distances in x and y (see Figure 11-5). This provides
the separate increments required in the x and y axes to move the instance forward by a total
distance of one pixel. Note that this means dx and dy will usually contain hypothetical fractions
of a pixel, but this is fine as they eventually add up to whole pixels.
Line 13 initializes the dist variable to start at 1. This variable will be used to keep track of
how far the instance has moved to ensure that it remains less than max_dist . Lines 15-21 now
pe r for m a while loop using this variable. A while loop is similar to a repeat loop, but rather than
repeating a fixed number of times, it keeps repeating while the expression in brackets evaluates
to true . Therefore, this particular while loop will keep repeating lines 17-20 while dist is less
than or equal to max_dist .
Line 17 uses the familiar place_meeting function to check if the instance would collide when
mov e d by dx pi xe l s i n the x a xi s an d dy pixels in the y axis (hence x+dx and y+dy ). If it would
collide, then the function returns true , ending the while loop and exiting the script immediately.
We don ' t a dd dx and dy to the current position until lines 18 and 19, so the instance will still be
at its old position (just before the collision) if the script returns on line 17.
 
Search WWH ::




Custom Search