Hello again! Sorry it took to long to reply; I got caught up in working on something job-related, but now that I am here I can show you how to do this; roughly.
I am going to assume you've already got a standard four-directional array of Kunio-style movements; if not, please share the file. Here is the coding Josef added, which will allow Kunio to jump.
In the Create event, add this code from the option in the "Control" tab:
- Code:
-
z=0 //or you can just call height
as=0 //air speed or falling speed
jump=0 //He's not jumping
You probably know the X and Y plotting from both grade school math class and the standard GM architecture, but it's 2D; jumping simulates a third dimension, which is why Josef added a new variable called "z" (Z is usually the accepted third dimension term in Math, when third dimensions are considered at all) and another called "jump". Both are at 0 to start.
Now we'll add the event where you actually press a button to jump; mine is spacebar (Note: Two-button functions are kind of tricky in GM, so I'd advise against doing the RCR-style "A+B" bit unless you're really, really nostalgic.) Make an event with that button being pressed, and add this code:
- Code:
-
if jump=0 //If he isn't jumping
{ //then
jump=1 //He's jumping
z+=1 //We must increase it, or he won't jump, don't modify
sprite_index=sprite_KunioStartJump image_speed=0.3 //It's temporary
as+=10 //Increase for higher jump
}
I'm actually a bit unclear as to how the z coordinate factors into this one, but it apparently is necessary, so do that. Overall, this makes sure the character is currently touching the ground, and if so shoots the character up into the air the height specified with as.
The coding is now set for the character to move up in mathematical terms, but it won't translate into graphics yet, so add a "Draw" event and put in this code:
- Code:
-
//Draw self
draw_sprite_ext(sprite_index,-1,x,y-z,image_xscale,image_yscale,image_angle,image_blend,image_alpha)
That looks cumbersome, but the only bit you need to note is the third and forth point--these are where the sprite appears based on defined x and y locations; x is left as the actual object's normal x coordinate, but y is changed to "y-z", which means the character will appear to move north when he's just moving up (hence jumping).
Now we've got the code for a character to jump up; all that's left is for that character to fall down again. For that, make a "Step" event and add this code:
- Code:
-
//Some gravity code
if(z>0){z+=as as-=1}
/*if he's landed then set the jump to 0, so he's not jumping anymore*/
if z<0 and as!=0 and jump=1 { jump=0 z=0 as=0 }
This bit is truly more advanced code than I'm capable of understanding, but I trust Josef; I know this works for my game.