i want lots of different sprites, buttons, etc. to do things like spin and grow/shrink. i can do that with an AnimationPlayer but i’d have to create a new animation and AnimationPlayer for every object.
What i think i want is to create something like Torque’s Behaviors - a generic object that i add as a child that takes the parent (either as an @export or maybe get_parent()) and uses that in the predefined animations is has. Then when i have objects i want to twirl or strobe or whatever i just attach the scene and bam, it’s done. Is that a thing i can do?
Alternately, is there a better way to achieve my goal of just attaching predefined behaviors to objects when building a level?
You could instantiate a AnimationPlayer with it’s tracks set to .. (aka it’s parent) as long as it’s animating properties that exist on the parent, scale and rotation are going to be common for 2D and 3D nodes.
Also Tweens are great for “unkown” quantities of things. An example I’d like to highlight from the docs
var tween = create_tween()
for sprite in get_children():
tween.tween_property(sprite, "position", Vector2(0, 0), 1)
This would animate all children towards the parent’s position, sort of like a vacuum for every object in just 3 lines. Along with set_loops() you could run a spinning or bouncing animation forever.
How do i do that? Is it just editing the text in the property track name? i tried that and it didn’t complain but it also didn’t work.
i created a Sprite2D, attached my Strober to it and told my component to print out that the animation name and that it was starting. It seems to have the right animation but when i run it i don’t see anything happen. No errors in the debugger.
Works quite well for me, I tried ..:position:x specifically, I also had to make the animation continuous instead of discrete manually, the right side of the tracks.
Your script is rather strange, I believe you only intend to use self.play(default_animation_name). Setting both current_animation and using play() will fight each other.
Oh! i got it working. In an unexpected way. i changed .. to . and it worked! Which seems crazy to me. But maybe on AnimationPlayers . means the parent of the AnimationPlayer already?
My class Stober is in it’s own scene. The AnimationPlayer is the root node of that scene (it’s not parented to a Node2D). i saved it as a prefab or whatever then dragged it onto an object in my test scene. So the tree in my test scene was TestNode2D->Strober and not TestNode2D->Strober->AnimationPlayer. i bet that’s where our examples were different.
But hey! It works. And i learned something new.
Now i’m going to see if i can get an equally easy solution (or better!) by using your idea for tweens.