• Hello [name]! Thanks for joining the GMC. Before making any posts in the Tech Support forum, can we suggest you read the forum rules? These are simple guidelines that we ask you to follow so that you can get the best help possible for your issue.

Question - Code Coding Error

V

Valor

Guest
Hi, I just started in game maker and ran into an issue with my animations and I can't seem to figure out what is happening. Essentially my sprite is running through its falling, jumping, and neutral animations right when it is created. As well when the arrow keys are pressed it will go into its run animation and will not stop. Below is all the code I have. Any help will be greatly appreciated!

//Get player input
//Tells us if pressing left arrow key, if we are, true = 1, false = 0
key_left = keyboard_check(vk_left); //yellow = function
key_right = keyboard_check(vk_right);
//pressed = dont check every fgrame, check if pressed in on this frame
//returns 1 or 0 depending on whether pressed or not
key_jump = keyboard_check_pressed(vk_space);

//Calculate movement
//var is temporary, only last 1 frame (1 loop of step event)
var move = key_right - key_left; //1 if moving right, -1 if moving left, if pressing both get 0 (no movement)

hsp = move * walksp; //hsp = horizontal speed, walksp = walk speed

vsp = vsp + grv;

if (place_meeting(x,y+1,oWall)) && (key_jump) //&& check both conditions (on floor and pressed jump)
{
vsp = -7; //for y negative = up, positive = down
}

//Horizontal collision
//If thing in brackets true do {} brackets
//Place meeting check if there is a collision between player object and oWall at coordinates x and y
//Cannot just set hsp to 0 as it will stop 3 pixels away if you try to move 4 pixels, so will not actually contact wall
if (place_meeting(x+hsp,y,oWall))
{
while (!place_meeting(x+sign(hsp),y,oWall)) //Ask no collision 1 pixel in direction were moving, sign returns 1 or -1 depending on variable + or -
{
x = x+sign(hsp); //increase x by one either to the right or left
}
hsp = 0;
}
x = x + hsp; //built in property of object, x coordiante in the room

//Vertical collision (same as above just different direction)
//Place meeting check if there is a collision between player object and oWall at coordinates x and y
if (place_meeting(x,y+vsp,oWall))
{
while (!place_meeting(x,y+sign(vsp),oWall)) //Ask no collision 1 pixel in direction were moving, sign returns 1 or -1 depending on variable + or -
{
y = y+sign(vsp);
}
vsp = 0;
}
y = y + vsp;

//Animation
if (!place_meeting(x,y+1,oWall))
{
sprite_index = sPlayerA; //sprite index tells what sprite to use
image_speed = 0; //how fast animation happens
if (sign(vsp) > 0) image_index = 1;
else image_index = 0; //+vsp = down, -vsp = up
}
else
{
image_speed = 1;
if (hsp == 0) //comparing
{
sprit_index = sPlayer;
}
else
{
sprite_index = sPlayerRun;
}
}
 
Last edited by a moderator:
Top