+1 vote

Hi!
I have bunch of dots, all of them are generated from tool and I would like to animate them one after another with little delay.
Right now I'm using yield function with timer. But it''s not perfect. Generally this dots are indicators of a button, when it's pressed they are changing color from first to the last but if player release the button they should back to original state. Sadly if you are pressing and releasing button rapidly this function seems to works incorrectly for my purposes (like few last dot's are starting early etc.).

Right now my code looks like this:

func turn_on():
    for child in activate_nodes:
        child.play()
        timer.start()
        yield(timer, "timeout")


func turn_off():
    for child in activate_nodes:
        timer.stop()
        child.stop()
        child.set_frame(0)

And here is sample scene:
enter image description here

How can I achieve this effect correctly? The best for me will be animating it from first to last but if button is released it's animating back to beginning state (even if only part of them will be activated).

in Engine by (226 points)

Let me see if I'm understanding what you're saying.

What you want is a way to turn the colour of one button into another colour, and with a little delay, do the same for the next one, and repeat for the rest.

And when they're released, put them back in the same color all at once?

Yes, mostly, it's need to be animated sprites because few next objects will be slightly different but action/reaction process will be still the same

1 Answer

0 votes

Ok then what you could do is give those button nodes an animation.
In Godot, you can not only animate variables/properties of nodes, but you can also animate method callbacks. This can be used here.

  1. Add a regular keyframe to change the sprite's current frame/colour
  2. Add another keyframe but this time in a callback track. Which calls the method change_adjacent.

change_adjacent looks like this.

func change_adjacent():
    has_changed = true
    #assuming they all share a common parent node-
    var max_index = get_parent().get_child_count() - 1
    if get_index()+1 <= max_index:
        var next_node = get_parent().get_child(get_index+1)
        if not next_node.has_changed: next_node.play("change_colour")
    if get_index()-1 >= 0:
        var prev_node = get_parent().get_child(get_index-1)
        if not prev_node.has_changed: prev_node.play("change_colour")

The animation called change_colour is the one I described earlier. Look in the docs to see how to make a callback track. To trigger this animation to begin with make some code to detect when the particular button is pressed. I'm sure you have something like that already. And as for releasing, just put all of these nodes in a group and call get_tree.call_group("your group name", "your reset method name")

by (3,853 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.