• 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!

SOLVED Playing idle animation to only face downward

G

gangkoso22

Guest
Hello all,

I'm still a noob and I'm posting here to get some help.

I#m using a sprite which has a total of 24 images, with 6 images facing each 4 directions.
What I'm trying to achieve is so that the sprite only facing downward, only playing the idle animation facing downward.
The problem is, the sprite plays the whole animation.

If I put image_index = 18; in the step event, the sprite does face downward but doesn't play the next animation.
So i came up with this.
GML:
currFrame = 0;
image_index = currFrame;
startFrame = 18;
maxFrame = sprite_get_number(sNPC_Josh);
image_speed = 0.2;
GML:
if (currFrame == 0)
{
    currFrame += startFrame
    while (currFrame <= maxFrame)
    {
        currFrame += 1;
    }
}
else
    {
        currFrame = startFrame;
    }
Any help would be very appreciated.
Thank you.
 

CloseRange

Member
so the idea of currFrame is the right idea.
In order to keep it in the correct range you wanna do a bit of math in the step event:
GML:
currFrame += image_speed; // increase frame count
image_index = (currFrame % 6) + startFrame; //
the mod 6 will make it cycle in sets of 6:
0 - 1 - 2 - 3 - 4 - 5 - 0 - 1 - 2- 3- 4- 5 - 0 . . . .
and then adding startFrame will offset it to the correct direction.
 
G

gangkoso22

Guest
so the idea of currFrame is the right idea.
In order to keep it in the correct range you wanna do a bit of math in the step event:
GML:
currFrame += image_speed; // increase frame count
image_index = (currFrame % 6) + startFrame; //
the mod 6 will make it cycle in sets of 6:
0 - 1 - 2 - 3 - 4 - 5 - 0 - 1 - 2- 3- 4- 5 - 0 . . . .
and then adding startFrame will offset it to the correct direction.
Hi.
Thank you so much for your reply.
So I did input what you suggested in the while loop like this
GML:
if (currFrame == 0)
{
    currFrame += startFrame
    while (currFrame <= maxFrame)
    {
        currFrame += image_speed; // increase frame count
        image_index = (currFrame % 6) + startFrame; //
    }
}
else
    {
        currFrame = startFrame;
    }
The sprite does indeed start from the 18th position, but after that it doesn't loop back to the 18th position again.
Instead it goes on the next position.
Did I do something wrong?
 
While doesn't make things happen "over time". While completely stops all processing (drawing, any code following the while, etc) until the entire while has completed. To put it another way while loops perform their entire loop cycle in a single step. The way you have it set up, it's impossible for the currFrame to be anything other than maxFrame or startFrame at any point in the game.
 
G

gangkoso22

Guest
While doesn't make things happen "over time". While completely stops all processing (drawing, any code following the while, etc) until the entire while has completed. To put it another way while loops perform their entire loop cycle in a single step. The way you have it set up, it's impossible for the currFrame to be anything other than maxFrame or startFrame at any point in the game.
Hiii...
Thank you for replying.
Now I understand the while loop better.
So instead of using while, what would be the best way to make it the entire loop cycle again?

I think I could make another sprite with only facing down animation, and use the state machine, to solve this.
But I really want to learn if there is another way to do it.
 

CloseRange

Member
the code I put should be in the step event but not nested inside anything.
what I put was the whole code...
it's really that simple with a bit of math
 
Top