• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

Legacy GM 8 direction with animation. need help?

B

begjan

Guest
Hello everyone. I need help. If anyone have done tutorial or code about 8 directional movement with idle, walk,
run sprites pls help me ^^

868161752.png
 

Nocturne

Friendly Tyrant
Forum Staff
Admin
Have you tried to do it yourself? If you have post what you've tried and we can help to improve on it... If you haven't then here's a very quick and easy way to do it...

1) Define sprites, one for each direction (so 8 sprites with animations), and one Idle sprite.
2) Create your object and assign it the idle sprite
3) In the STEP event set the sprite_index to the idle sprite
4) Still in STEP, after you set the sprite index, check for keys something like this:

Code:
if keyboard_check(vk_left)
{
sprite_index = spr_left;
if keyboard_check(vk_up) sprite_index = spr_upleft;
if keyboard_check(vk_down) sprite_index = spr_downright;
}
5) Repeat the above code for the other three directions changing the sprites appropriately.
 
B

begjan

Guest
yes. i did. but mine is not working 100$. for example: when i press vk_up + vk_right keys same time then obj_player moves direction 45 with sprite that facing direction 45. But when i release both key maybe one of the vk_up and vk_right key may released last then idle sprite that facing direction 45 will not work because of last one of vk_ up or vk_right key released. there is no other way to keep release these 2 keys same time in GM: Studio. Nocturne. maybe you have to see my code that i wrote down. thanks

image_speed = 0.175;
//image_speed = speed/5
speed=0;

/////////////////////////////////////////////////////////////
if (keyboard_check(vk_right) && keyboard_check(vk_up))
{
direction=45;
speed=3;
sprite_index=spr_walk_right_up;
}
else if (keyboard_check(vk_right) && keyboard_check(vk_down))
{
direction=315;
speed=3;
sprite_index=spr_walk_right_down;
}
else if (keyboard_check(vk_left) && keyboard_check(vk_up))
{
direction=135;
speed=3;
sprite_index=spr_walk_left_up;
}
else if (keyboard_check(vk_left) && keyboard_check(vk_down))
{
direction=225;
speed=3;
sprite_index=spr_walk_left_down;
}
/////////////////////////////////////////////////////////////
else if keyboard_check(vk_right)
{
direction=0;
speed=3;
sprite_index=spr_walk_right;
}

else if keyboard_check(vk_up)
{
direction=90;
speed=3;
sprite_index=spr_walk_up;
}
else if keyboard_check(vk_left)
{
direction=180;
speed=3;
sprite_index=spr_walk_left;
}
else if keyboard_check(vk_down)
{
direction=270;
speed=3;
sprite_index=spr_walk_down;
}
////////////////////////////////////////////////////////////
if (keyboard_check_released(vk_right) && keyboard_check_released(vk_up))
{
sprite_index=spr_idle_right_up;
}
else if (keyboard_check_released(vk_right) && keyboard_check_released(vk_down))
{
sprite_index=spr_idle_right_down;
}
else if (keyboard_check_released(vk_left) && keyboard_check_released(vk_up))
{
sprite_index=spr_idle_left_up;
}
else if (keyboard_check_released(vk_left) && keyboard_check_released(vk_down))
{
sprite_index=spr_idle_left_down;
}
else if keyboard_check_released(vk_right)
{
sprite_index=spr_idle_right;
}
else if keyboard_check_released(vk_up)
{
sprite_index=spr_idle_up;
}
else if keyboard_check_released(vk_left)
{
sprite_index=spr_idle_left;
}
else if keyboard_check_released(vk_down)
{
sprite_index=spr_idle_down;
}
 
A

AnimusRex

Guest
What I have is a create event for the player character which defines;
Code:
image_index=0;
image_speed=0;
imageNumber=0;
Then in the step even for the character I have;
Code:
//Code for turning on the spot
if (ismoving == false)
{
if (keyboard_check_pressed(vk_right))
    {
        image_index += 2;
    }
if (keyboard_check_pressed(vk_left))
    {
        image_index -= 2;
    }
}

//Code for moving in whichever direction is forward facing
if (keyboard_check(vk_up) and image_index = 0)
        {
        ismoving = true;
        movetimer = gridsize;
        speedy = -movespeed;
        speedx = 0;
        }
       
if (keyboard_check(vk_up) and image_index = 2)
       {
        ismoving = true;
        movetimer = gridsize;
        speedy = 0;
        speedx = movespeed;
        }
       
if (keyboard_check(vk_up) and image_index = 4)
        {
        ismoving = true;
        movetimer = gridsize;
        speedy = movespeed;
        speedx = 0;
        }
       
if (keyboard_check(vk_up) and image_index = 6)
        {
        ismoving = true;
        movetimer = gridsize;
        speedy = 0;
        speedx = -movespeed;
        }

//Code for moving backwards opposite to the facing direction
if (keyboard_check(vk_down) and image_index = 0)
        {
        ismoving = true;
        movetimer = gridsize;
        speedy = movespeed;
        speedx = 0;
        }
       
if (keyboard_check(vk_down) and image_index = 2)
       {
        ismoving = true;
        movetimer = gridsize;
        speedy = 0;
        speedx = -movespeed;
        }
       
if (keyboard_check(vk_down) and image_index = 4)
        {
        ismoving = true;
        movetimer = gridsize;
        speedy = -movespeed;
        speedx = 0;
        }
       
if (keyboard_check(vk_down) and image_index = 6)
        {
        ismoving = true;
        movetimer = gridsize;
        speedy = 0;
        speedx = movespeed;
        }
Your image_index values will be different according to how you have your sprite animated, and lots of my other variables are to do with grid based movement. The important ones are

Code:
{
if (keyboard_check_pressed(vk_right))
    {
        image_index += 2;
    }
if (keyboard_check_pressed(vk_left))
    {
        image_index -= 2;
    }
}
Which cycles through the different animation frames I have for the sprite's facing.

Right now I am doing it in 2 increments instead of 1 as I only have 4 directional movement. Sorting out the grid base for 8 directions will be it's own headache :D

Hope that helps, if you have any questions let me know.
 
B

begjan

Guest
oh i am kind of new and trying to understand you code ^^ what about idle?
 
D

DaleTron

Guest
Assign idle to your obj like nocturne said, thats what Ive found to be the most common method,

I mean do stuff your own way if you want, but from what ive seen is most people assign standing or idle to the object, and since your idle is your founding stance, as in everything pretty much goes from idle, then back to idle:i understand why it's the norm animation assigned to the object-since you can only assign one sprite to an object you gotta choose wisely.

There's some dope code on this page!

I found a really cool tutorial on 8 directional movement a couple days ago when I was debating between 4 and 8.

Bewarned, dude has a very cliche voice, but dont let that discourage you, he explains some good good on how the 360 works in game maker a lil, and some dope movement code that has like 1 if statement. maybe 2.

WHEN HE EXPLAINS THE SPECIAL DIRECTION, BOTTOM-RIGHT, HE ACCIDENTLY REFERS TO THE RIGHT KEY AS THE LEFT KEY AND IT TOOK ME LIKE 3 TIMES WATCHING THE VID TO SEE IT WASNT MY MISTAKE

Heres a pretty good article I found on stackExchange too, theres a lil trick in there if you have different sprites for walk/run.

as for walk run, if i ever do implement a run mechanic: I'm just going to double the image speed,
 
Top