• Hello [name]! Thanks for joining the GMC. Before making any posts in the Tech Support forum, can we suggest you read the forum rules? These are simple guidelines that we ask you to follow so that you can get the best help possible for your issue.

Question - Code How to make the player only jump when the animation is played?

G

gabrielcamargo

Guest
Newbie here. As the title says, how to make the player only jump when the animation is played? And sorry for my bad english, but even so I think you can understand.
 
Usually animations are triggered by some kind of action, not the other way around. Is there some kind of jump button in your game?
If you mean to add some kind of delay between pressing and the character jumping, you can use image_index to get the specific frame it's on. Then do something like:
GML:
if (image_index == 4) {
    ySpeed = -4;
}
 

Sabnock

Member
Newbie here. As the title says, how to make the player only jump when the animation is played? And sorry for my bad english, but even so I think you can understand.
Are you able to post the code you have so far?

As @Octtopus_Tophat says a jump would normally be triggered by a button press but there are also in game events that may cause this as well, a spring pad or an explosion for example but their point remains the same, an external event triggers the player to jump and then you set an animation for that event.

Normally you would have some simple gravity system in a game that pulls the objects to the ground. such as ...
GML:
vspeed += grv;
// vspeed is a variable that holds the current speed your object is travelling at in the vertical direction.
// and grv is a variable that holds your gravitational constant 0.4 for example.

// Check for event that triggers a jump state
if (button_pressed) vspeed = -jump_speed // where jump_speed is a variable that holds the speed you want you char to jump at

// Collision code goes here and if true move the player object to be up tight to the collision object and then set the vspeed to zero.
if (place_meeting(x, y + vspeed, o_wall)) { // Checks for a collision in the direction of vspeed
    while(!place_meeting(x, y + sign(vspeed), o_wall)) {  // If there has been a collision move the player up tight to the object it has collided with.
        y += sign(vspeed);
    }
    vspeed = 0; // Collision forces the player to stop.
}

// Then once gravity, jump and collisions have been checked and dealt with you add the vspeed value to the y position.

y += vspeed;
There are more checks and events that need to be dealt with but this is basically a simple jump and gravity system that allows you to create a jumping system.
 
Last edited:
G

gabrielcamargo

Guest
Are you able to post the code you have so far?

As @Octtopus_Tophat says a jump would normally be triggered by a button press but there are also in game events that may cause this as well, a spring pad or an explosion for example but their point remains the same, an external event triggers the player to jump and then you set an animation for that event.

Normally you would have some simple gravity system in a game that pulls the objects to the ground. such as ...
GML:
vspeed += grv;
// vspeed is a variable that holds the current speed your object is travelling at in the vertical direction.
// and grv is a variable that holds your gravitational constant 0.4 for example.

// Check for event that triggers a jump state
if (button_pressed) vspeed = -jump_speed // where jump_speed is a variable that holds the speed you want you char to jump at

// Collision code goes here and if true move the player object to be up tight to the collision object and then set the vspeed to zero.
if (place_meeting(x, y + vspeed, o_wall)) { // Checks for a collision in the direction of vspeed
    while(!place_meeting(x, y + sign(vspeed), o_wall)) {  // If there has been a collision move the player up tight to the object it has collided with.
        y += sign(vspeed);
    }
    vspeed = 0; // Collision forces the player to stop.
}

// Then once gravity, jump and collisions have been checked and dealt with you add the vspeed value to the y position.

y += vspeed;
There are more checks and events that need to be dealt with but this is basically a simple jump and gravity system that allows you to create a jumping system.
thank you guys, after researching a lot I arrived at the result in a very simple way. when the player presses the space key the animation is played, when he releases it (through the key up event), the player jumps! ;)
 
Top