*SOLVED* Problem in my code, can u help me?

I

its_Johnny

Guest
hi everyone,
My name is Johnny, its the first time I write on this forum.

I'm experiencing some problems on my game code and I hope that someone can help me:

since I coded the animations, my character does not move if the key arrows are pressed, while it moves only using space bar (it jumps and it can move left-right only during the jump, when it touches the floor it can't)
in addiction its sprite does not change its orientation altho it should (it always faces the same direction, altho it should face left while the left arrow key is pressed).

can you help me please? I'm supposed to finish this videogame for January to go to the university I want and I would really appreciate some help!

i include my code in case you need it, I'm pretty sure the problem is on the "animation" section (at the end) because if I delete it, the game just works fine; any help? thank you so so so much!

//get player imput
key_left = keyboard_check(vk_left);
key_right = keyboard_check(vk_right);
key_jump = keyboard_check_pressed(vk_space);

//calculate movemet
var move = key_right - key_left;

hsp = move * walksp;

vsp = vsp + grv;

if (place_meeting(x,y+1,O_wall)) && (key_jump)
{
vsp = -7;
}

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

//vertical collision
if (place_meeting(x,y+vsp,O_wall))
{
while (!place_meeting(x,y+sign(vsp),O_wall))
{
y = y + sign(vsp);
}
vsp = 0;
}
y = y + vsp;

//Animation
if (!place_meeting(x,y+1,O_wall))
{
sprite_index = S_playerA;
image_speed = 0;
if (sign(vsp) > 0) image_index = 1; else image_index = 0;
}
else
{
image_speed = 1;
if (hsp == 0)
{
sprite_index = S_player;
}
else
{
sprite_index = S_player_run
}
}
if (hsp != 0) image_xscale = sign(hsp);
image_xscale = 0.1;
 

CloseRange

Member
The problem is most likely how the sprites are made. Collisions are calculated using collision masks. The dimensions of the mask are determined by the sprite, if you have an object that changes sprites, this can be a problem if the sprites collision masks arn't the same size.
The simple fix is to make sure that the collision mask is always the same no matter what sprite you are on so at the top of the code add this:
Code:
mask_index = S_player;
as for the player not being oriented correctly:
Code:
if (hsp != 0) image_xscale = sign(hsp);
image_xscale = 0.1;
you set image_index to be based on hsp, then right after set it to a whole other value.
Just change it to this:
Code:
if (hsp != 0) image_xscale = sign(hsp) *.1;
sign is always 1 or -1 in this case, so if you multiply it by .1 it will always be .1 or -.1
 
I

its_Johnny

Guest
The problem is most likely how the sprites are made. Collisions are calculated using collision masks. The dimensions of the mask are determined by the sprite, if you have an object that changes sprites, this can be a problem if the sprites collision masks arn't the same size.
The simple fix is to make sure that the collision mask is always the same no matter what sprite you are on so at the top of the code add this:
Code:
mask_index = S_player;
as for the player not being oriented correctly:
Code:
if (hsp != 0) image_xscale = sign(hsp);
image_xscale = 0.1;
you set image_index to be based on hsp, then right after set it to a whole other value.
Just change it to this:
Code:
if (hsp != 0) image_xscale = sign(hsp) *.1;
sign is always 1 or -1 in this case, so if you multiply it by .1 it will always be .1 or -.1
hello, sorry but I had some problem using this website and I found your message only today. I just would like to thank you for helping me and sorry if I reply you only today :)
 
Top