Game Development Reference
In-Depth Information
9.
Now, edit the Step event of obj_flynn and include these two new lines at the end:
if( object_index != obj_flynn_air || y < peak_height )
peak_height = y;
The first part of this condition will ensure that Flynn's peak_height variable is updated
to match his y position, when he is not in the air state object (so not jumping or
falling). The second part adds an exception to this condition using the or ( || ) operator
so that peak_height is updated while jumping or falling if Flynn's current y position is
higher than the previous peak height. So as a result, the peak_height variable will
always contain Flynn's highest y position since he last stood or held onto solid ground.
10. Finally, for obj_flynn , edit the Draw event and add new cases for setting the bend and
recover sprites in the usual way. It doesn't matter what order the new states come in
the list of cases, but don't forget to do both left- and right-facing sprites.
11. Open obj_flynn_land and edit the Key Press, Space event. Change the line that sets
vspeed to -22 so that it sets vspeed to JUMP_SPEED instead (the constant already includes
the minus, so there is no need to include it here).
12. Do the same for the Key Press, Space event of obj_flynn_ice . We now have one
constant that can be changed to globally alter the jump speed, which is a bit neater.
13. Open obj_flynn_air and edit the Create event. Change the existing code to set gravity
to 3.5 and then add three new lines that set image_speed , image_index , and then
jump_count all to the value 0 . Now that we have two frames for our falling sprite, we
need to control the index and speed of the animation manually.
14. Edit the End Step event of obj_flynn_air . Most of this code is included in a condition
that only occurs when Flynn is falling ( vspeed > 0 ). Remove the line that sets gravity
to 2 (we're not bothering with the extended jump for Flynn, as the double jump
replaces it). Insert the following lines of code in its place:
1: var fall_dist;
2: fall_dist = y - peak_height;
3:
4: if( fall_dist > FALL_DIST_FATAL )
5: image_index = 1;
6: else
7: image_index = 0;
So when Flynn is falling, this calculates how far Flynn has fallen so far and selects the
appropriate subimage according to whether the fall is “safe” or “fatal.” We'll use a
similar approach to decide what to do at the end of a fall later.
15. In the same code, remove the last two lines that check to see if Flynn is jumping and
always set the image_index to 0 if he is. We're going to use different subimages for
jumping and double jumping, and we've already made sure the subimage won't
change on its own by setting image_speed to 0 in the Create event.
16. Edit both the Keyboard, Left and Keyboard, Right events of obj_flynn_air and change
the amount added or subtracted from hspeed to DRIFT_ACCEL (which we set to 3). This
will make the drifting mechanic appropriately stronger for the larger Flynn.
At this point, you might think that all we need to do now to support a double jump is add a
Key Press, Space event to obj_flynn_air that launches Flynn into the air based on the value of
jump_count . Unfortunately, it's not as simple as that, because of the slightly unpredictable way
that the Key Press events work while changing between instances.
 
Search WWH ::




Custom Search