Player Gets Stuck In Wall When Switching Sprites

A

AlphaRedDragon

Guest
So Im Pretty Sure im not the first to ask this, but for some reason, when my player changes direction with his walk animation, he gets stuck in the wall, ive checked to make sure that the animation is the same width as the still player but it still didnt help, is there some specific collision system for this cause my current one is not working

current collision system


// horizantal collission

if (place_meeting(x+hsp,y,obj_collide))
{
while(!place_meeting(x+sign(hsp),y,obj_collide))
{
x += sign(hsp);
}
hsp = 0;
}
//vert collision

if (place_meeting(x,y+vsp,obj_collide))
{
while(!place_meeting(x,y+sign(vsp),obj_collide))
{
y += sign(vsp);
}
vsp = 0;
}

If you have any questions, or need any additional info, let me know because i cant make prgress in my game until this gets sorted, thanks
 

Nocturne

Friendly Tyrant
Forum Staff
Admin
Okay, the general reason for this is that when you switch direction you are also flipping the image_xscale of the sprite. Your code doesn't show this, but if you ARE doing that then stop! Simply DRAW the sprite flipped but don't change the image_xscale value, and instead have a custom variable that you use along with draw_sprite_ext() to get the same effect.
 
A

AlphaRedDragon

Guest
Okay, the general reason for this is that when you switch direction you are also flipping the image_xscale of the sprite. Your code doesn't show this, but if you ARE doing that then stop! Simply DRAW the sprite flipped but don't change the image_xscale value, and instead have a custom variable that you use along with draw_sprite_ext() to get the same effect.
The way i am doing it is by having a direction variable and 2 seperate sprites for left an right. i hav never heard of draw_sprite_ext(), can you go a bit further into how it works?
 

Nocturne

Friendly Tyrant
Forum Staff
Admin
Okay, if you are using two separate sprites and getting this error then it would suggest that the bounding boxes are different for each sprite... so check that. As for using variables and draw_sprite_ext, it would work something like this:

Code:
// CREATE EVENT
spr = spr_right;

// STEP EVENT
if keyboard_check(vk_right)
{
spr = spr_right;
}
if keyboard_check(vk_left)
{
spr = spr_left;
}

// DRAW EVENT
draw_sprite_ext(spr, image_index, x, y, image_xscale, image_yscale, image_angle, image_blend, image_alpha);
So, as you can see, we have substituted the built in variable "sprite_index" for the custom variable "spr", meaning that the actual sprite assigned to the instance NEVER changes, but what is drawn will. You can then assign a sprite to the instance with the mask you require and it won't change as the game is played. Note that you can swap out any of the built in variables (like the image_xscale one as I suggested originally) and use custom variables to affect what is drawn without affecting the base properties of the instance.
 
A

AlphaRedDragon

Guest
Okay, if you are using two separate sprites and getting this error then it would suggest that the bounding boxes are different for each sprite... so check that. As for using variables and draw_sprite_ext, it would work something like this:

Code:
// CREATE EVENT
spr = spr_right;

// STEP EVENT
if keyboard_check(vk_right)
{
spr = spr_right;
}
if keyboard_check(vk_left)
{
spr = spr_left;
}

// DRAW EVENT
draw_sprite_ext(spr, image_index, x, y, image_xscale, image_yscale, image_angle, image_blend, image_alpha);
So, as you can see, we have substituted the built in variable "sprite_index" for the custom variable "spr", meaning that the actual sprite assigned to the instance NEVER changes, but what is drawn will. You can then assign a sprite to the instance with the mask you require and it won't change as the game is played. Note that you can swap out any of the built in variables (like the image_xscale one as I suggested originally) and use custom variables to affect what is drawn without affecting the base properties of the instance.


wow, i dont know how i didnt think of that, thats amazing, thank you so much, you just basically fixed my game, ill be sure to use that from now on
 
A

AlphaRedDragon

Guest
Okay, if you are using two separate sprites and getting this error then it would suggest that the bounding boxes are different for each sprite... so check that. As for using variables and draw_sprite_ext, it would work something like this:

Code:
// CREATE EVENT
spr = spr_right;

// STEP EVENT
if keyboard_check(vk_right)
{
spr = spr_right;
}
if keyboard_check(vk_left)
{
spr = spr_left;
}

// DRAW EVENT
draw_sprite_ext(spr, image_index, x, y, image_xscale, image_yscale, image_angle, image_blend, image_alpha);
So, as you can see, we have substituted the built in variable "sprite_index" for the custom variable "spr", meaning that the actual sprite assigned to the instance NEVER changes, but what is drawn will. You can then assign a sprite to the instance with the mask you require and it won't change as the game is played. Note that you can swap out any of the built in variables (like the image_xscale one as I suggested originally) and use custom variables to affect what is drawn without affecting the base properties of the instance.

one more question, how do i get it to play animations using this?
 

Nocturne

Friendly Tyrant
Forum Staff
Admin
one more question, how do i get it to play animations using this?
That would depend... If you are simply setting the image_speed then using that builtin variable in the draw event should still work. If it's not then you should post your full code for us to look at...
 
A

AlphaRedDragon

Guest
That would depend... If you are simply setting the image_speed then using that builtin variable in the draw event should still work. If it's not then you should post your full code for us to look at...

What do you mean by builtin variable?



Oh, built in variable.

heres what i got in my code

create:
spr = spr_playerv2_right


Step:


move = move_left + move_right;



if(move = 0)
{
if(dir = 1)
{
spr = spr_playerv2_right

}

if(dir = -1)
{
spr = spr_playerv2_left

}






}

if(move = 1)
{
spr = spr_player_walking_right
dir = 1;

}

if(move = -1)
{
spr = spr_player_walking_left
dir = -1

}

Draw:
draw_sprite_ext(spr,image_index,x,y,1,1,0,c_white,1);
 

Nocturne

Friendly Tyrant
Forum Staff
Admin
You don't appear to be setting the image speed anywhere, which is the variable that controls how sprites are animated... something like this maybe?

Code:
if(move = 0)
{
image_speed = 0;
if(dir = 1)
    {
    spr = spr_playerv2_right
    }
if(dir = -1)
    {
    spr = spr_playerv2_left
    }
}

if(move = 1)
{
spr = spr_player_walking_right
dir = 1;
image_speed = 0.5;
}

if(move = -1)
{
spr = spr_player_walking_left
dir = -1
image_speed = 0.5;
}
 
A

AlphaRedDragon

Guest
You don't appear to be setting the image speed anywhere, which is the variable that controls how sprites are animated... something like this maybe?

Code:
if(move = 0)
{
image_speed = 0;
if(dir = 1)
    {
    spr = spr_playerv2_right
    }
if(dir = -1)
    {
    spr = spr_playerv2_left
    }
}

if(move = 1)
{
spr = spr_player_walking_right
dir = 1;
image_speed = 0.5;
}

if(move = -1)
{
spr = spr_player_walking_left
dir = -1
image_speed = 0.5;
}


I dont know why, but i cant get it to work,i did what you recomended and di a bit of trial and error myself but it wont work
 
A

AlphaRedDragon

Guest
@Nocturne The image_xscale bug has been fixed for some time now, unless there is a new bug I haven't encountered yet.
i dont think its a bug with the image_xscale, im trying to use the draw_sprite_ext() to make the players _animation play, but i cant get it to work
 

TheouAegis

Member
I know @AlphaRedDragon . It's just I've seen Noct stress not using image_xscale, which stemmed from a bug that plagued GM for since its inception due to some shoddy programming. It was fixed one or two years ago (not sure which release, but one of the bug mods told me he was informed it was fixed). With the old bug, GM would negate certain values associated with the sprite_index, but it seemed there was a typo in one of their codes which resulted in an incorrect calculation of the bounding box size. It caused a lot of headaches for people for many years and even broke some finished games that hadn't been properly debugged.
 
A

AlphaRedDragon

Guest
I know @AlphaRedDragon . It's just I've seen Noct stress not using image_xscale, which stemmed from a bug that plagued GM for since its inception due to some shoddy programming. It was fixed one or two years ago (not sure which release, but one of the bug mods told me he was informed it was fixed). With the old bug, GM would negate certain values associated with the sprite_index, but it seemed there was a typo in one of their codes which resulted in an incorrect calculation of the bounding box size. It caused a lot of headaches for people for many years and even broke some finished games that hadn't been properly debugged.

oh, i see, well, glad they fixed it
 

Fredrik

Member
If you're having problems with collision you could always just go into the sprite file and press "Modify mask" and change so the mask wouldn't be the exact same as the sprite, right? or even in the object set the mask to be a other sprite from the displaying sprite.
 
P

Pylyf

Guest
Okay, if you are using two separate sprites and getting this error then it would suggest that the bounding boxes are different for each sprite... so check that. As for using variables and draw_sprite_ext, it would work something like this:

Code:
// CREATE EVENT
spr = spr_right;

// STEP EVENT
if keyboard_check(vk_right)
{
spr = spr_right;
}
if keyboard_check(vk_left)
{
spr = spr_left;
}

// DRAW EVENT
draw_sprite_ext(spr, image_index, x, y, image_xscale, image_yscale, image_angle, image_blend, image_alpha);
So, as you can see, we have substituted the built in variable "sprite_index" for the custom variable "spr", meaning that the actual sprite assigned to the instance NEVER changes, but what is drawn will. You can then assign a sprite to the instance with the mask you require and it won't change as the game is played. Note that you can swap out any of the built in variables (like the image_xscale one as I suggested originally) and use custom variables to affect what is drawn without affecting the base properties of the instance.

Hi, can you send me this code for vertical collision or just how to make it please? I edited by myself but it didnt worked. :(
 
Top