Why is my tween not Ping Ponging?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By indy2005

Hi

I am trying to make a sprite pulse, but it expands then contracts but does not repeat. It would be great to have a “Ping Pong” option in Godot (Construct 3 has it, and it is very useful).

What am I doing wrong, it seems that once “completed” has fired for a tween instance, it doesn’t get fired again? Either that or I am scaling from 1.0 to 1.0 for some reason.

I have added 2 tweens to the node for now, TweenExpand and TweenContract - and when one completes, I initiate the other so it should expand and contract. At the moment, it does it only once (expand->contract->stop).

func _ready():
	# When a balloon is created, we choose a color from one of the animations
	# and we set a random rotation
	# and we tween the scaling up and down by a random amount
	add_to_group("balloons")
	
	var rng = RandomNumberGenerator.new()
	rng.randomize()
	tweenScale = rng.randf_range(1.1,1.8)
	tweenTime = rng.randf_range(1.2,2.0)
	
	tweenExpand.interpolate_property(self, "scale",
		self.scale, Vector2(tweenScale, tweenScale), tweenTime,
		Tween.TRANS_LINEAR, Tween.EASE_IN_OUT)
	tweenExpand.start()
	
	var animation_index = rng.randi_range(0, len(animations)-1)
	anims.animation = animations[animation_index]
	
	anims.frame = 0
	anims.stop()
	
	


func _on_TweenExpand_tween_completed(object, key):
	tweenContract.interpolate_property(self, "scale",
		self.scale, Vector2(1.0,1.0) , tweenTime,
		Tween.TRANS_LINEAR, Tween.EASE_IN_OUT)
	tweenContract.start()


func _on_TweenContract_tween_completed(object, key):
	tweenExpand.interpolate_property(self, "scale",
		self.scale, Vector2(tweenScale, tweenScale), tweenTime,
		Tween.TRANS_LINEAR, Tween.EASE_IN_OUT)
	tweenExpand.start()

Only thing I can think of is your tweenContract signal is not properly connected. It should work on the first look as you expect.


Btw you don’t need new RandomNumberGenerator for float range, there is a rand_range function available.

And for clarity… you may call _on_TweenContract_tween_completed in func _ready, so you DRY :wink:

Reloecc | 2020-09-16 09:15

But it does contract once, it just doesnt ever expand again. So it must be connected correctly to contract at all. Not sure what you mean by call it in _ready? Its a signal, why would I call it manually?

indy2005 | 2020-09-16 12:02

:bust_in_silhouette: Reply From: njamster

Your code works fine for me.

it expands then contracts but does not repeat

Judging form this description I would say you haven’t (properly or at all) connected _on_TweenContract_tween_completed - then you’d get that exact behavior!

It would be great to have a “Ping Pong” option in Godot

Tweens will get a complete overhaul for Godot 4.0, see this issue. Feel free to propose that feature. It’s not too hard to implement it yourself though.