[SOLVED] Every 3 seconds, change animation

Hello All!

I’m hoping someone can help me solve a problem.
I’m looking to change the animation of an object every 3 seconds.
So let’s say I have an enemy who has both animation 0 and 1, I’m looking to change it from 0 to 1 to 0 again and so forth every 3 seconds. I’ve tried all sorts of things without any luck. Once the enemy changes to animation 1, he gets stuck at one and doesn’t go back to 0 even if a condition is True.

Thanks!

Hi,

Have you tried putting the images together under 0, set it to loop, and change the number next to the little stopwatch to 3?
That would make it change continuously, but as slow as you want it.

Probably you’re doing the classic: if animation is 1 >> set animation to 0, if animation is 0 >> set animation to 1.
And in that order to get stuck in 1 :slight_smile:

If it’s the problem, don’t worry, is a common issue, both events are running in the same frame. Here are two ways to fix it: [url]on/off Button for Background music]
If you go for the second option (useful if you switch two values only) you just need one action.

Merge the two animations and set the time between frames to 3 seconds as suggested above is a good idea too, you can pause animations and set frames manually through events if you need it.

I think I’m even more confused now. Staying up all night probably isn’t good for me.

I need to figure out a way to loop the condition.

setInterval(function(){

	if(animation == 0) {
    	animation = 1;
    } else {
    	animation = 0;
    }

}, 3000);

I need it to work in that sort of way. So every 3 seconds, it checks a value and changes it accordingly.

Or better yet, how can I access the animation property in JavaScript?

Check the link I sent, the first option emulates the “else” block with a variable conveniently called “else” :slight_smile:
Replace the cursor condition by the timer condition, remove the two actions playing and stopping music, and use animations 0 and 1 instead of “play” and “stop” :wink:

Another way is to use a variable to check if one of the actions ocurred and then don’t do the another.

Example:

[code]Conditions:
timer “change_animation” value is greater than 3 seconds
Actions:
Do =0 to variable “check”


                SubConditions:
                          animation of "object" =0
                Actions:
                          Do =1 to variable "check"
                          Do =1 to animation of "object"

                SubConditions:
                          variable "check" =0
                          animation of "object" =1
                Actions:
                          Do =0 to animation of "object"[/code]

This will do the trick.

I tested this and it somewhat works? After 3 seconds, it changes animation but then once it changes animation from 0 to 1, it flickers super quickly between 0 and 1 instead of every 3 seconds.

I tried that and after 3 seconds, it changes to animation 1 and stays at one.

Edit: I looked back over the logic and changed some values around and got the same result from the last test where it starts off good and after 3 seconds, it changes to animation 1, but then immediately starts flickering quickly between 0 and 1.

This is what I have.

Here’s a YouTube video of the problem: youtu.be/ZlLNm2Nur0s

Almost perfect! :smiley:
Your missing the action to reset the timer, put it in the first event, above or under the action that sets the variable = 1.

I would like to offer a different approach. I’m just going to write it out in pseudo-code. The basic idea, is instead of this if/else structure, just use addition, and then when you increase the animation number to 1 greater than the maximum animation, set it back to 0. This is scaleable easily to more than 2 animation loops you want to switch between:

{At scene initialization} {reset timer “animSwitchTimer”}

{If timer "animSwitchTimer" > 3.0 seconds} { reset timer "animSwitchTimer" D0 + 1 to the current number of animation BoogieCat } { The current number of animation BoogieCat > Value(LastAnimationNum) } { Do = 0 to the current number of animation BoogieCat}

The basic idea is that you just keep adding 1 to the animation number until you are greater than the last animation number (in the case where there are 2 animations, you would set LastAnimation to 1, so once the animation number gets incremented to 2, we reset back to 0; if you had 5 animations, you would set LastAnimation to 4, etc). This would allow for 3, 4, or even 20 animations (or whatever is the max number of animations that GDevelop supports, not sure what that is), as you just keep adding one until you reach the max and then ‘wrap around’.

Update: I’m not entirely certain that GDevelop will let you treat the number of an animation like it’s a variable, and do comparisons. I’m still learning GDevelop. I’m thinking about this like C++ or Java code, and this is what I would do in those languages, where essentially everything can be treated like a variable.

Update 2: This almost works. I tried to test this, and I suspect that it won’t let me increase the animation number to 1 greater than the max animation number. It cycles through the animations until it gets to the last, but the step to reset the value to 0 never fires. That could probably be worked around by using another variable to add 1 to each time, and then add 1 to the animation. That would allow the variable used for the test to overflow the animation number.

Ok, got a chance to make a second attempt at my addition and wrap back to 0 approach to this. I think this is beautiful. I made advantage of the fact that if one event resets a timer, and you add a second event that is conditional on that timer, it will fail. This is a beautiful solution, IMHO. Also, in the way of further refinements, I made the timer into an object timer on the object I’m animating, and 2 variables on that object - an AnimMax (the maximum animation number to loop to) and AnimSecs (which is how long to stay on each animation):

Yeah, the problem is it’s not reseting the timer.

This is a working example: timer_and_conditions.rar (1.63 KB) Take a look at it, it changes between a warm or cold background color every 3 seconds.

Have you considered the solution I gave a couple posts above? I think you’ll find it’s quite a good solution. It even allows you to easily put different animation timers on different sprites, and put as many different animations as you like.

Sorry I haven’t had the chance to post back, but I just tested the reset timer (delete timer from memory) and it works perfectly! I don’t know how I missed that…

Thank you guys so much, you are the best! How do I mark this as solved?

Just edit the title of this post to:

“[SOLVED] This post title”