2D games have a tendency to look flat and dead. One way to make them come alive is with idle animation and casual motion. Today, we’ll make our generators throb like a heartbeat when they activate and stop throbbing when they deactivate.
This entire action takes place in the piece.gd script. We want it to throb when we activate it and stop when we deactivate it so…
Add Variable
...
var tween : Tween
...By placing the tween at the top of the script where all those texture arrays are, it’s outside the scope of the function and will survive the function once we activate it.
Activate_Generator() Revision
...
throb()At the very bottom of the function, that’s all we need to start everything rolling.
Throb()
func throb():
tween = create_tween()
tween.set_loops()
tween.set_trans(Tween.TRANS_SINE)
var duration : float = 1.5
tween.tween_property($Sprite2D, "scale", Vector2(0.9,0.9), duration)
tween.tween_property($Sprite2D, "scale", Vector2(1.0,1.0), duration)So we set the global tween variable to a new create_tween().
Then we tell it to set_loops() which is different than before. This makes the tween keep going even after we leave the function rather than just running once and stopping.
Set_trans tells it how to TRANSition between the two actions we’re about to take using the TRANS_SINE format. There are several options. I think this looks better to my eye for the heartbeat I’m trying to make, but if you want a different look you can change it.
I wasn’t sure how long the time duration needed to be so I set a single variable so both the throb up and down are matching.
Lastly, we have the 2 changes to the scale property that make up the piece image sprite throb. (Make sure that it’s pointing to the image sprite of your piece not to the piece as a whole or some other part of the piece scene.) The tween is smart enough to loop these two back-to-back: 80% to 90% and back to 80% until we tell it to stop.
Deactivate_Generator() Revision
...
stop_throbbing()Just like activate_generator(), we only need this to stop the throbbing.
Stop_Throbbing()
func stop_throbbing():
if tween != null:
tween.kill()Since this is specific to the piece code, it only kills the throbbing tween on the specific piece we’re referencing, not on all pieces.
Test and Verify
This is easy to test since as soon as you make your first generator, it’ll start throbbing. If you don’t like the way it looks, you can change it.
To make the throbbing stop, just make it generate enough to run out of items and deactivate.
This one was short and simple compared to that last one. Next time, I’ll go back to the ReqDoc to add bonus points via special gem Rocks.

