1.2 Movement

Player

Initial Variables

I'm first going to go ahead and define some variables whenever the player is created in my game. These will relate to how movement can occur and having these will let me change and experiement with different values that affect how fast I move, fall and jump.

Initial variables for my player

Keyboard Inputs

Within my step event, the first thing I want to go ahead and do is map some set keyboard bindings to move my player object. I'll do so by creating some boolean flags that raise when their corresponding key is pressed. For now I've set it to use the arrow keys for left, right and jump (there's no duck atm).

As an alternate option, I've also gone ahead and mapped the WSAD keys accordingly since for the first stage I'm using the mouse for projectile shooting from the player. This layout makes more sense if you're using the mouse for right-handed players (WSAD + mouse) but I've also provided support for the arrow keys again that maybe some left-handed players might appreciate.

In further iterations I'll obviously want players to be able to select their own key bindings, but for now this will do.

Mapping key inputs for the player object

Movement

Now for movement, I'll be utilising the state of the keys that are being pressed as defined previously and applying that onto the actual object using its speed attributes from the Create event earlier.

Action

Implementation

move

The difference of the right and left key states

fall speed

Effect of gravity onto player

move speed

Product of walk speed and state of movement

jump

Constant reduction in fall speed provided the key is pressed and the player is grounded

GML code for player movement

Last updated

Was this helpful?