Windows [SOLVED] sprite_index breaking the code

R

Rackover

Guest
Hello,

I'm having my first steps on GameMaker and after following the tutorial to create a basic platformer game, everything works perfectly : I can move my character, jump, and I correctly collide with walls and ground.

But if try to add some conditions to change my sprite using sprite_index, for an unknown reason, my character gets stucks in the ground and cannot move until I press Jump and then, I can move while i'm in the air, but not on the ground.

Simply deleting the lines related to sprite_index fixed it.

After I studied the GM documentation and found no answer, I had a hard time suceeding to register on this website to ask this question :

Could anyone provide me more information about sprite_index and the way it interacts with physics or anything ? No one seems to mention the way it's working on my side.

Here is the working code :
Code:
///Get the player's input
//Get the player's input
key_right = keyboard_check(vk_right);
key_left = -keyboard_check(vk_left);
key_jump = keyboard_check_pressed(vk_space);
//React to inputs
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
}
//Horizontal Collision
if (place_meeting(x+hsp,y,OBJ_wall))
{
    while(!place_meeting(x+sign(hsp),y,OBJ_wall))
    {
        x += sign(hsp);
    }
    hsp = 0;
}
x += hsp;
//Vertical 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;

By adding these 9 lines at the end, the bugs I talked about do happen :
Code:
if (vsp!=0){
   sprite_index=SPR_player_jumping;
}
else if (hsp!=0){
    sprite_index=SPR_player_running;
}
else{
    sprite_index=SPR_player;
}
 

FrostyCat

Redemption Seeker
Without a pre-defined mask, sprite_index also defines the collision zone of an instance. If the new sprite doesn't cover the exact same set of pixels as the old one, the extra pixels run the risk of latching into blocks. Animated sprites can also have the same problem while changing frames.

Make a solid-fill, one-frame rectangular sprite with the same size and origin as your character, then go into Object Properties and set Mask to it. This ensures that your collision zone never gets extra pixels.
 
R

Rackover

Guest
Can't
Without a pre-defined mask, sprite_index also defines the collision zone of an instance. If the new sprite doesn't cover the exact same set of pixels as the old one, the extra pixels run the risk of latching into blocks. Animated sprites can also have the same problem while changing frames.

Make a solid-fill, one-frame rectangular sprite with the same size and origin as your character, then go into Object Properties and set Mask to it. This ensures that your collision zone never gets extra pixels.
All my sprites actually are the same size. Can't I make the collision be just a square, even if the character has a particular shape ? I do not need precise collisions. I just checked and all their masks are square-shaped rectangles, all the same size.
 

Ralucipe

Member
Make sure that:
  • All your player sprites are the exact same size
  • All your player sprites have the exact same origin
  • All your player sprites have "full image" checked in their collision mask properties
This is a cheap and quick fix, if you don't want the mask to take the full image you'll have to ensure that the collision masks have identical dimensions
 
Top