Game Development Reference
In-Depth Information
We k n ow the Key Press, Space event must have already been triggered in another object
(perhaps obj_zool_land ) in order to reach the obj_zool_air state. So you wouldn't expect it to be
triggered again until the Space key was released and then pressed again. However, when you
change the instance in a Key Press event, Game Maker can actually end up triggering the Key
Press event again—in the same step—for the object that you are changing into.
If this was always the case, then it wouldn't be unpredictable, but unfortunately it depends
on the order in which objects are stored internally in Game Maker. So changing into
obj_flynn_air in the Key Press event of obj_flynn_land ca l l s the Key Press, Space event in both
obj e cts. Ye t, cha n g i n g i n to obj_flynn_land in the Key Press event of obj_flynn_air only triggers
it once for the original object (presumably because the other object has already been processed
for Key Press events by that point).
This means we need a solution that explicitly controls whether the Key Press event is
counted for the double jump, as we can't rely on Game Maker to do this for us.
17. Edit the Key Release, Space event of obj_flynn_air . Delete the line that sets gravity
(and the comment) and include the following two lines instead:
if( jump_count == 0 )
jump_count = 1;
18. Now, add a new Key Press, Space event for obj_flynn_air and include an Execute
Code action that contains the following lines of code:
1: if( jump_count == 1 )
2: {
3: sound_play( snd_jump );
4: vspeed = JUMP_SPEED;
5: image_index = 1;
6: state = FSTATE_JUMP;
7: jump_count = 2;
8: }
So now the sequence of events for a double jump works like this:
The player triggers the Key Press, Space event in one of the other state objects that
changes it into obj_flynn_air and calls the Create event.
The variable jump_count is set to 0 in the Create event of obj_flynn_air , whi ch
mean s the Key Press, Space event we've just created won't do anything if it gets
automatically called again by Game Maker.
The player eventually triggers the Key Release, Space event by releasing the Space
key and jump_count gets set to 1 .
The player can then press the Space key again and this time the Key Press, Space
event will succeed and allow Flynn to do a double jump. The jump_count variable is
also set to 2 so Flynn can't jump again until he leaves this state object and returns
back to it (that is, he lands on something and jumps again).
It's quite an involved solution, but it doesn't rely on the order in which objects are created, so it
should work in all different situations. Next up, we need to cope with what happens when Flynn
l a n ds fr om a he i g ht, ma k i n g hi m r e cov e r or di e de pe n di n g on the tota l di sta n ce he ha s fa l l e n .
 
Search WWH ::




Custom Search