Windows Player Movement: Leaping

F

Freedom2Fight

Guest
Hello. I would like to request your help.

I am making a platformer. I am new to this but I am very eager to learn.

I want the playable character to able to "leap". So whenever I press the [jump button] the character will [jump and move forward] in the [direction they are facing] [a certain number of pixels]. Please see illustration below.


I call it "the absence of color leaping" - deep huh?

The code I provided below allows the player to jump and control their direction mid-air. Metal Slug was like that right? Good times. I, uh, don't want that... Its not the feel I'm looking for.

Here is the code and you might already have seen it in the tutorials:

Code:
key_right= keyboard_check(vk_right);
key_left= -keyboard_check(vk_left);
key_jump = keyboard_check_pressed(vk_space) ;

move = key_left + key_right;
hsp = move*movespeed;

if (vsp < 10) vsp+=grav;

if (place_meeting(x,y+1,obj_wall1))
{
vsp = key_jump * -jumpspeed
}

x += hsp;
y += vsp;

//Horizontal Collision//
if (place_meeting(x+hsp,y,obj_wall1))
{ while (!place_meeting(x+sign(hsp),y,obj_wall1))
{x+= sign(hsp);}
hsp=0}
x += hsp;

//Vertical Collision

if (place_meeting(x,y+vsp,obj_wall1))
{ while (!place_meeting(x,y+sign(vsp),obj_wall1))
{y+= sign(vsp);}
vsp=0}
y += vsp;

//animation

if (move!=0)image_xscale = move;
if (place_meeting(x,y+1,obj_wall1))
{
if (hsp!=0) sprite_index = sprite3; else sprite_index = sprite_idle;
}
else
{
if (vsp<0) sprite_index = sprite_jump ; else sprite_index = sprite_fall
}
Whenever I try to make adjustments things become super weird. For example as soon as the game starts, the player character gets yanked out of the room or whenever press space the player character moves forward and upward, forever - a double jump to eternity. Or they just slide forward.

How would you go about it?

Looking forward to your responses.
 
Last edited by a moderator:
A

Aura

Guest
First of all, you are performing x += hsp and y += vsp twice. And I don't guess that's what you want.

And why are you setting image_xscale to move? You must have wanted to use sign(move) instead.

Making the player leap while moving is pretty easy. Simply make it leap if the player is moving and touching the ground.

Code:
if (x != xprevious)
{
   if (place_meeting(x, y+1, obj_wall1))
   {
      vsp = -jumpspeed;
   }
}
 
F

Freedom2Fight

Guest
Thank you for your response Aura. However I fear that I wasn't clear with my first post. For that I apologize. I have reviewed and edited the original post should you wish to see it. And that goes for all those who wish to read this thread.

To those who will be kind enough to help, I thank you in advance. And will likely thank you after.
 
A

Annoyed Grunt

Guest
Simply do not set hsp = move * movespeed unless you are on the ground.
 
F

Freedom2Fight

Guest
Thank you for your response Annoyed Grunt. I'm not sure how to go about your suggestion and so I ended up having the playable character stop moving mid-air.

I don't want the player to have any control their direction when they jump. I want the playable character to, when the jump button is pressed, launch themselves in the air whilst moving forward automatically then hopefully gravity will help get the character to land on the ground.


I'm thinking it has to do with hsp and vsp but when I try to change things, weird unintended things happen.
 

johnwo

Member
Try something like this:

In the Create Event add:
Code:
inAir = false;
Step event:
Code:
key_right= keyboard_check(vk_right);
key_left= -keyboard_check(vk_left);
key_jump = keyboard_check_pressed(vk_space) ;

move = key_left + key_right;
hsp = move*movespeed;

if (vsp < 10) vsp+=grav;

if (place_meeting(x,y+1,obj_wall1))
{
    vsp = key_jump * -jumpspeed
    if vsp < 0 then inAir = true; // We're jumping
}

if inAir then hsp = movespeed*image_xscale;

x += hsp;
y += vsp;

//Horizontal Collision//
if (place_meeting(x+hsp,y,obj_wall1)) {
    while (!place_meeting(x+sign(hsp),y,obj_wall1)) {
        x+= sign(hsp);
    }
    hsp=0;
}
x += hsp;

//Vertical Collision

if (place_meeting(x,y+vsp,obj_wall1))
{ 
    while (!place_meeting(x,y+sign(vsp),obj_wall1)) {
        y+= sign(vsp);
    }
    vsp=0;
    inAir = false; // Landed on ground
}
y += vsp;

//animation

if (move != 0) image_xscale = move;
if (place_meeting(x,y+1,obj_wall1))
{
    if (hsp!=0) sprite_index = sprite3; else sprite_index = sprite_idle;
}
else
{
    if (vsp<0) sprite_index = sprite_jump ; else sprite_index = sprite_fall
}
The added variable simply check if the player is jumping, and if he/she is jumping it sets the hsp to be movespeed in the direction the player is facing.

Should work with little to no modification.

Cheers!
 

Alexx

Member
Here is the code with the adaptions you wanted applied an in example:
Example GMZ
Feel free to learn from it, but don't reuse the sprites.

Here is the adapted code:
Code:
//general movement
key_right= keyboard_check(vk_right)*!inAir;
key_left= -keyboard_check(vk_left)*!inAir;
key_jump = keyboard_check_pressed(vk_space) ;

move = key_left + key_right;
hsp = move*movespeed;

if (vsp < 10) vsp+=grav;

if (place_meeting(x,y+1,obj_wall1))
{
    vsp = key_jump * -jumpspeed
    if vsp < 0 then inAir = true; else inAir=false; // We're jumping
}

if inAir then hsp = movespeed*image_xscale;

x += hsp;
y += vsp;

if (place_meeting(x,y+vsp,obj_wall1))
{
    while (!place_meeting(x,y+sign(vsp),obj_wall1)) {
        y+= sign(vsp);
    }
    vsp=0;
    inAir = false; // Landed on ground
}
y += vsp;

//animation

if (move != 0) image_xscale = move;
if (place_meeting(x,y+1,obj_wall1))
{
    if (hsp!=0) sprite_index = spr_player_walk; else sprite_index = spr_player_idle;
}
else
{
    if (vsp==0) sprite_index = spr_player_idle ; else {sprite_index=spr_player_jump;
        image_index=5;}
}
if (move!=0 && place_meeting(x,y+1,obj_wall1)) sprite_index=spr_player_walk;
move=0;
hsp=0;
 
F

Freedom2Fight

Guest
I would like to thank johnwo for suggesting the creation of another variable and providing relevant changes to the movement code. Although it did cause the player to walk forever after landing in the direction they are facing when the jump button is pressed. Which is cool, a game can be made out of that mechanic.

I would also like to thank Alexx for making the necessary changes to the code to make it work as it was intended. Alexx also provided a project file! Very nice.

There is however slight animation problem, at the peak of the jump there is a frame that plays that makes the player's movement a bit off. I think its a frame from the idle animation.

To those who plan to use this code be aware that you need to change a few things if your animation includes a sprite for falling.

Other than that the code works.

I am sincerely thankful, this is one of the issues that has been bugging me a lot. Yes, one of 'em but those are for another topic.

Lastly, I would like to thank those that responded as it helped propel this discussion forward.

Well done all!
 
Top