sprite animation

S

supermasher/ti

Guest
so i Am trying to animate my sprite movement and i got it to do the animation .But i don't know how to make it go into an idle pose when it stops moving.here are pic's of my code Slide1.PNG Slide2.PNG Slide3.PNG Slide4.PNG
i tried adding a macro but then it was glitching. i need help!
 
S

supermasher/ti

Guest
if you need a better expiation what is happening is my sprite plays the animation for UP LEFT RIGHT DOWN when i press them on the key board but when i let go the animation keeps going for the direction i was on last an i need to find out how to make an the player sprite go into an idle pose/standing for each direction P.Si am new to game maker and GML
 

SamSam

Member
Hey supermasher/ti,

The best thing I can recommend you is this video.
You will understand everything you need about animation (I hope).

And if you are a beginner, as I am, I also strongly recommend you to subscribe to his channel and watch every single of his videos ^^

EDIT : sorry, realized I wrote "recommend" the french way
 
Last edited:
S

supermasher/ti

Guest
i watched the video but i got confused because his game was set up different (platformer) wail i am doing a (top down) game i tried doing something like this but it did not work here is what i tried test code.png


But when i did this i got a message saying that I had a malformed if statement .I am confused.
 

SamSam

Member
I know GML is pretty permissive on the code but I can see a few risky things :

If your variable Direction is a string, you shoud write
Code:
Direction = "DOWN";

I think it's ok with GML but 2 "=" is better for conditions as far as I know
Code:
if (Direction == "DOWN"){ }

There is no ";" after a condition / some ";" are missing
Code:
if (Direction == "DOWN") {
     if (Yaxis == 0) {
             sprite_index = D_p_IDLE;
     }
     // ...
}
I hope this will help ! If not you will have to tell us more, by sending your error report for exemple.

By the way, you should use the "insert code" option to communicate code with us, it makes it more readable and we can copy/paste it to modify it. :)
 
S

supermasher/ti

Guest
OK I am going to try something different now because this dose not seem to work with the movement code i already have .this a video of what I have so far.

https://photos.google.com/album/AF1QipN1LY4Ms0MzPgYgK2RalmCksmEHPMkDDQhn4oyh

So as you can see i plays the walking animation but it doesn't play an idle animation P.S. I am only trying to code the animation for walking Down right now



here is my OBJ_Player code looks like this
Presentation1.png


Create//:

Code:
// @description Inisloiz

XAxis = 0; // left/right movement
YAxis = 0; // up/down movement
Speed = 2; // how fast we move

Direction = DOWN;
Action = IDLE;

View[RIGHT,IDLE] = v_p_right;

View[UP,IDLE] = v_p_up;

View[LEFT,IDLE] = v_p_left;

View[DOWN,IDLE] = v_p_down;
Step//:

Code:
 /// @description

XAxis = keyboard_check(vk_right) - keyboard_check(vk_left);
YAxis = keyboard_check(vk_down) - keyboard_check(vk_up);

if (XAxis > 0)
    Direction = RIGHT;
else if (XAxis < 0)
    Direction = LEFT;
    
if (YAxis > 0)
    Direction = DOWN;
else if (YAxis < 0)
     Direction = UP;
    
var _direction = point_direction(0, 0, XAxis, YAxis);
var _length = Speed * (XAxis != 0 || YAxis != 0);
//show_debug_message(string(_length));

XAxis = lengthdir_x(_length, _direction);
YAxis = lengthdir_y(_length, _direction);

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

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

//Chang the sprit
sprite_index = View[Direction,Action];

// movment code

if (Direction == "DOWN") {
     if (YAxis == 0) {
             sprite_index = D_p_IDLE;
     }
     //...
}










but in my scripts i also have and they look like this 

Macros//:

[CODE]// direction
#macro RIGHT 0
#macro UP 1
#macro LEFT 2
#macro DOWN 3
// action
#macro IDLE 0
so let me know if there is any thing that i can add change to make this work or if you think i should try something else just remember i am only working on the direction down right now.
 
S

supermasher/ti

Guest
oh i wrote the macros and step together on accident just so you know .
 

SamSam

Member
I tested your code and it's working for me... At least it animates so I think it's working (your link is off).
But I find your code a bit confusing. The array defines which sprite to use when the player doesn't move, but you're still using sprite_index = D_p_IDLE; in the step event.

I had a hard time understanding why you had so much going for a such simple need so here is how I would have done it (100% working for the core of your problem) :


create
Code:
idle = 1;
walking = 0;

up = 3;
right = 2;
down = 1;
left = 0;

Action = idle;
Direction = down;

View[1,3] = v_p_upIdle;
View[1,2] = v_p_rightIdle;
View[1,1] = v_p_downIdle;
View[1,0] = v_p_leftIdle;

View[0,3] = v_p_upWalking;
View[0,2] = v_p_rightWalking;
View[0,1] = v_p_downWalking;
View[0,0] = v_p_leftWalking;
step
Code:
if (keyboard_check(vk_up) || keyboard_check(vk_right) || keyboard_check(vk_down) || keyboard_check(vk_left)) {
    Action = walking;
    if (keyboard_check(vk_up)) {Direction = up;}
    if (keyboard_check(vk_right)) {Direction = right;}
    if (keyboard_check(vk_down)) {Direction = down;}
    if (keyboard_check(vk_left)) {Direction = left;}
} else {Action = idle}

sprite_index = View[Action, Direction];
Maybe this is not the best way to do it, but it is the most simple and readable way I could find. If you have the 8 sprites, the animation of the player will always match his action and direction.

Just in case, make sure the speed of your sprites in the sprite nodes is not set to 0.

Hope this will help a little !
 
S

supermasher/ti

Guest
now i tried this and here is a video of what happens

https://photos.app.goo.gl/h92wWRdnoV8qb8Tt5

so I am wondering if I need to do any thing with my macros

Macro//:

Code:
// direction
#macro RIGHT 0
#macro UP 1
#macro LEFT 2
#macro DOWN 3
// action
#macro IDLE 0
Also I was not sure what you meant when you said "make sure the speed of your sprites in the sprite nodes is not set to 0."
 

SamSam

Member
With the code from my last post you don't need macro anymore. As far as I know macros are mostly a way to make the code more readable (again) in some cases.

If you look at the sprite node :
8.png

You have this part :
9.png
This is in french here but in english "Vitesse" means "Speed" and "Images par seconde" means "frames per second". As you can see I have set the speed to 0, so if I display this sprite it won't animate. If it is set to any number other than 0 it will animate, and you can modulate the animation speed with
Code:
image_speed = 0.5; // the animation will be half speed
image_speed = 2; // the animation will be double speed
For the problem you show in the video :
Maybe this is when you press multiple keys, like up and right simultaneously. I you use the code from my last post, this is only a foundation, now you have to tune it to fit your gameplay :)

But you may also check if the reference point is logically placed in all your sprites. For instance on the floor under the feet. If it is placed once down and once up, since the reference point is placed at the same point in relation to the object, you may run into that kind of problem.
10.png
 
Top