vsp = 0; // (vertical speed - set to empty because initially, it is just a container)
jumpspeed = 7; // (the amount of speed that will be added to the object in the up direction when we jump)
movespeed = 4; // (the amount of pixels that we move left and right when we press those keys)
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Add Event
Step (This means that these events carried out by the object will happen every single frame of the game)
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Drag in code box into actions panel
Add comment // get the player's input
Check player movement inputs - this is done by assigning the results of keyboard checks to variables.
Type this into the code box:
key_right = keyboard_check(vk_right);
// if key is pressed, this variable will equal 1, otherwise it will equal 0
key_left = -keyboard_check(vk_left);
// minus before keyboard check - this mens it will equal 0 or -1
key_jump = keyboard_check_pressed(vk_space);
// pressed means the computer checks to see if the button was pressed, but it
// was not pressed the frame before - i.e, literally just pressed jump button.
// Means you can't just hold the jump key down to continually jump
// React to inputs
move = key_left + key_right;
// -1 if left key is pressed, 1 if right key is pressed and 0 if neither is pressed
// If both are pressed it still equals 0
hsp = move * movespeed;
// As move (shown above )equals -1 or 1 and move speed = 4, this should set hsp in this instance to either - 4 or 4
if (vsp < 10) vsp += grav;
// vsp will add 0.2 to itself, up to a maximum of 10. This controls the falling rate
// of the player, otherwise the onscreen sprite would just pick up speed with every frame it falls
// with no limit
if (place_meeting(x,y+1,obj_wall))
// This is asking "Is there an object called wall 1 pixel below the player"?
{
vsp = key_jump * -jumpspeed;
// If jump key is pressed, this should set the jumpspeed to -7. If not, 0.
}
// Collision - Ordinarily in GM if you've collided with something, it's already too late
// What you need to do is get into the midst and logic of saying to the computer we are "ABOUT"
// to collide, do something NOW
// For example, our hsp here = 4. If we are three pixels from a wall, we need to ask the computer
// are we about to hit a wall to our right?
// If we are 3 pixels away, we move 1 pixel at a time until we can't move any further without hitting
// the wall and we reduce our speed to 0
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Horizontal collision
if (place_meeting(x+hsp,y,obj_wall))
{
while (!place_meeting(x+sign(hsp),y,obj_wall))
// sign will return 1 or -1 depending on whether hsp is positive or negative
// so this would equate to x left or x right (-1 left or 1 right)
// sign will multiply this by the horizontal speed -1 * 4 or 1 * 4. This gives the current amount of
// pixels to move left or right for the horizontal speed.
// So what you are saying is, if the player is not touching obj_wall, keep on moving 1 pixel
// until it does. At which point reduce speed to 0 and stop. Same principle goes for vsp below.
{
x += sign(hsp);
}
x += hsp;
// add horizontal speed to x co ordinate - move sprite left or right
// Vertical collision
if (place_meeting(x,y+vsp,obj_wall))
{
while (!place_meeting(x,y+sign(vsp),obj_wall))
// sign will return 1 or -1 depending on whether hsp is positive or negative
{
y += sign(vsp);
}
vsp = 0;
}
y += vsp;
// add vertical speed to y co ordinate - move sprite up and down
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
This code works perfectly and solves the problem - pixel perfect gravity. I went on to apply this code to every surface in the game to create a solid, interactive world.
No comments:
Post a Comment