• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

Need some help on sprite collision and animation.

P

PhoxFroot

Guest
I was recently working and understanding on how to code movement and collision in GameMaker when I ran into an issue with one of my sprite/images being activated. Basically, if the players vertical speed is decreasing, it would activate a image of falling, but the problem is, when the player lands on the object of collision(the floor), he gets stuck in the place he was supposed to collide with, thus making it impossible for him to move. I can provide the code to those who can help. I just started using GML and GameMaker in general so I'm new to all of this. Please help.

Code

Create instance on character:

///declare_Variables
grav = 0.8;
hsp = 0;
vsp = 0;
jumpspeed = 5;
movespeed = 4;


step instance on character:

///player_Movement

//Player Input
key_right = keyboard_check(ord ('D'));
key_left = -keyboard_check(ord ('A'));
key_jump = keyboard_check_pressed(ord ('W'));

//Reaction
move = key_left + key_right;
hsp = move * movespeed;
if (vsp < 10) vsp += grav;

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

//H-Collsion
if (place_meeting(x+hsp,y,obj_wall))
{
while(!place_meeting(x+sign(hsp),y,obj_wall))
{
x += sign(hsp);
}
hsp = 0;
}
x += hsp;

//V-Collision
if (place_meeting(x,y+vsp,obj_wall))
{
while(!place_meeting(x,y+sign(vsp),obj_wall))
{
y += sign(vsp);
}
vsp = 0;
}
y += vsp;

image_speed = 0.4;
if (keyboard_check_pressed (vk_shift))
{
image_speed = 1;
movespeed = 10;
}
if keyboard_check_released (vk_shift)
{
image_speed = 0.4;
movespeed = 4;
}

//Animation
if (move != 0) image_xscale = move;
if (place_meeting (x, y+1, obj_wall))
{
if (move != 0) sprite_index = spr_player_walk; else image_index = spr_player_idle;
}
else
{
if (vsp < 0) sprite_index = spr_jump; else image_index = spr_player_idle;//this ones 💩💩💩💩ing me up
}
 
Last edited by a moderator:

obscene

Member
Two things.

First Is this image a different size from your normal animation? If so, what might not be a collision for one might be a collision for another causing an "unexpected" collision. You might consider just making a separate sprite (Just a rectangle) and setting that as the mask.

Also, It lookg like you are confusing sprite_index and image_index. sprite_index would equal a sprite asset, like you are doing. An image_index is number representing the current frame of animation within a sprite (0,1,2,3,etc..)
 
Top