N
Nirwanda
Guest
Following a tutorial by Shaun Spalding, I created a controlled jump but I've run into a bug: if I hold the jump key just the right amount of time, the character will get stuck on the ground:
Could it be a bounding box issue? Or is there something wrong with my code? or uhh..What else could it be?
Here's the code:
Create event:
Step event:
EDIT: Added 1 pixel to the bottom bounding box of each sprite and now it seems to be fixed, but she looks like she's floating in the air. Any middle ground solutions?
Here's the code:
Create event:
Code:
image_speed = 0.2;
grav = 0.25;
hsp = 0;
vsp = 0;
jumpspeed = 8;
movespeed = 8;
global.direction=1
Code:
key_jump = keyboard_check_pressed(ord("Z"));
key_jump_held = keyboard_check(ord("Z"));
// HORIZONTAL MOVEMENT
if (keyboard_check(vk_left) && keyboard_check(vk_right)) ||
(!keyboard_check(vk_left) && !keyboard_check(vk_right))
// holding both directional keys or neither of them - stop movement
{
if hsp >= 1 hsp -= 1; // reduce speed
else if hsp <= -1 hsp += 1; // reduce speed
else if hsp != 0 hsp = 0; // stop
}
else if keyboard_check(vk_right) // holding RIGHT key
{
global.direction = 1; // set direction
hsp = min(hsp + 1, 8); // increase speed; limit to 8
}
else if keyboard_check(vk_left) // holding LEFT key
{
global.direction = 0; // set direction
hsp = max(hsp - 1, -8); // increase speed; litit to -8
}
// WHEN ON GROUND
if (place_meeting(x, y+1, obj_ground))
{
// JUMP
if key_jump vsp = -jumpspeed // pressing jump button
// SET SPRITE (STANDING/RUNNING ANIMATION)
if hsp == 0 // when idle
{
if global.direction == 1 sprite_index = spr_rose_idleR; // idle: right
else sprite_index = spr_rose_idleL; // idle: left
}
else if hsp > 0 sprite_index = spr_rose_runR; // running: right
else if hsp < 0 sprite_index = spr_rose_runL; // running: left
}
// WHEN IN MID-AIR
else // when in mid-air
{
// GRAVITY
vsp = min(vsp + grav, 10) // increase falling speed; limit to 10
// SET SPRITE (JUMPING ANIMATION)
if vsp <= 0 // going UP
{
if global.direction == 1 sprite_index = spr_rose_jumpR; // up-right
else sprite_index = spr_rose_jumpL; // up-left
}
else // falling DOWN
{
if global.direction = 1 sprite_index = spr_rose_fallR; // down-right
else sprite_index = spr_rose_fallL; // down-left
}
}
if vsp<=0 && (!key_jump_held) vsp = max(vsp,0)
// COLLIDE WITH WALLS (HORIZONTALLY)
if (place_meeting (x+hsp, y, obj_ground))
{
while(!place_meeting(x+sign(hsp), y, obj_ground))
{
x += sign(hsp);
}
hsp = 0;
}
// COLLIDE WITH WALLS (VERTICALLY)
if (place_meeting(x, y+vsp, obj_ground))
{
while(!place_meeting(x, y+sign(vsp), obj_ground))
{
y += sign(vsp);
}
vsp = 0;
}
// MOVE CHARACTER
x += hsp;
y += vsp;
Last edited by a moderator: