• Hey! Guest! The 36th GMC Jam will take place between February 27th, 12:00 UTC - March 2nd, 12:00 UTC. Why not join in! Click here to find out more!

Legacy GM help on moving sprite

EZTALES

Member
hey yall,
i wrote this little bit of code but i need it for 7 sprites instead of only 3 in this case, i assume its a easy fix but i have no idea on how to do it any help is great!
Code:
Step Event
if keyboard_check_pressed(vk_right) and image_index != 2 then image_index += 1 //moves player right
if keyboard_check_pressed(vk_left) and image_index != 0 then image_index -= 1 //moves player left
 
Off the top of my head I think this will do it:

if keyboard_check_pressed(vk_left)
{
if image_index > 0
{image_index -= 1}
}

if keyboard_check_pressed(vk_right)
{
if image_index < (image_number - 1)
{image_index += 1;}
}

Image_number is the amount of frames the currently used sprite has, and Image_index starts at zero so the final frame is one less than image_number. If you find that you don't get the last frame then remove the - 1 from "image_number - 1"

I should ash though - you put "moves player left / right etc", but this will only cycle through the animation frames, and not physically move an object. Is that what you want?
 

EZTALES

Member
Off the top of my head I think this will do it:

if keyboard_check_pressed(vk_left)
{
if image_index > 0
{image_index -= 1}
}

if keyboard_check_pressed(vk_right)
{
if image_index < (image_number - 1)
{image_index += 1;}
}

Image_number is the amount of frames the currently used sprite has, and Image_index starts at zero so the final frame is one less than image_number. If you find that you don't get the last frame then remove the - 1 from "image_number - 1"

I should ash though - you put "moves player left / right etc", but this will only cycle through the animation frames, and not physically move an object. Is that what you want?
yes, this is what im after, ill gie a try and get back to you, thanks!
 

EZTALES

Member
Off the top of my head I think this will do it:

if keyboard_check_pressed(vk_left)
{
if image_index > 0
{image_index -= 1}
}

if keyboard_check_pressed(vk_right)
{
if image_index < (image_number - 1)
{image_index += 1;}
}

Image_number is the amount of frames the currently used sprite has, and Image_index starts at zero so the final frame is one less than image_number. If you find that you don't get the last frame then remove the - 1 from "image_number - 1"

I should ash though - you put "moves player left / right etc", but this will only cycle through the animation frames, and not physically move an object. Is that what you want?
this does work! the only thing left that I want is when you have made it to the final sprite, (sprite 7) loop back to the very first one. I only want this for the last sprite and not the first as well so you don't go backward. thanks!
 
Top