Cycling through animations on a sprite

Before proceeding, please use the forum search feature at the top of the page to check if your question has already been answered.

How do I…

I have a sprite with multiple animations, and I want it to change to the next when I hit the S key, but once cycled through all of them it goes back to the first. I can get it to cycle through them all just fine, but if I try to add anything that checks for the last animation title to reset it back to #0, nothing works (like, even the old working part doesn’t suddenly!)

What is the expected result

The item (hat) will cycle through all of it’s animations (variants) when you hit S, in a loop.

What is the actual result

the first part, hitting s to +1, works fine so long as I have no other event checking for the second part (final animation). as soon as I add that part, even if I have no event triggering from the condition, it won’t work.

Related screenshots


also tried this as two separate events not as a nested one.

Instead of just adding the animation number, create an object variable. Check the dropdown below if you do not know how to add an object variable:

How to Add Object Variable?

In the list of objects, right click the object you want to add an object variable to, then select “Edit object variables”. After that, click “add a variable” and give it a name like “AnimationNumber” and leave the default value as 0.

Then increase the object variable by 1, and set the animation number of the object to that variable. Make sure to add an event that will reset the variable if the number is above the maximum possible (for me the maximum is 5). See the below example:

Hope that helps!

Oongus

I’m not sure about using a variable to keep track of the animation number - it’s just adding another layer of complexity.

If there are only 2 animations then what’s happening is understandable. The first subevent sets the animation to the hat01-2, and the second subevent will immediately set it to the first animation, so it’ll appear like it doesn’t work.

One way around this is to add a blank third/final animation (say hat01-3). So then the animation sequence goes from hat01-1 to hat01-2, and then from hat01-2 to hat01-3 which will not be displayed as it immediately gets set to hat01-1.

On thing to keep in mind though - these animation changes are happening at around 60 fps, so it will seem like a blur. You’ll be best to put some kind of delay if the ‘S’ key is held down for a while.

I’ve also tried it on release instead of press as well, as to avoid that issue (or try to) but the same problems arose sadly, so I assumed it wasn’t due to a sudden flip back and forth.

Correct, because it’s caused by that last event - the animation is set to the last one, and because it’s the last one, it gets reset to the first one immediately, so the last animation is never displayed. So my suggestion of a dummy last animation will be a quick solution here.

unfortunately that didn’t work. Maybe I’m not explaining it correct, and there won’t always be just 2 animations but sometimes more.

So, let’s say I have an item like the hat. I want it if the mouse is hovered over said hat, and presses a button (s), it changes to the next animation (a different style), then if they press s again, it will go to the next. Like it will stop at each one, and not move past that animation (sort of setting it’s state I guess?)

So, I can get it to do it. Once. then it won’t work again. even if I try sub-events where the first “conditions” are just hovering over it and releasing the button, then have the other subs as “Animation does not = whatever#” move +1, it will only ever do that once, or just that.

If I add another subevent in there instead looking for “animation DOES = whatever#” to make it then go back to the first one, it will never do this. Like this sub event check never happens, no matter what.

I may have to go the variable route sadly.

I just feel like I’m explaining this badly. I guess I’ll use colours even though I’m not just tint changing. One hat spite, has 4 animations, each a different colour. I want it to change colour (animation) with a button press, and stay that colour. Each press, cycles to the next. When it reaches the last animation, it then resets back to the first colour.

It’s weird that I can’t get this work as I figured it would be a simple +1, then check for last animation and reset back… but nadda. hm.

Instead of using a name to check if the last animation has been reached, could you use the number of animations as a check? If so, then this could be a viable solution:

that seems to work for what I was after mostly. Thankyou!