Move Sprite from current position to new random position

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

Hi everyone,

my Sprite jumps from its current position to another random position quite nicely:

func _ready():

		randomize()
		yield(get_tree().create_timer(rand_range(0.5,2)), "timeout")

		var x_position = rand_range(-10, 4)
		var y_position = rand_range(-7, 4)
		position = Vector2(x_position, y_position)

		_ready()

I’d like that jumping to change into smooth moving and I thought a Tween would do the trick, so I tried this:

func _ready():

		randomize()
		yield(get_tree().create_timer(rand_range(0.5,2)), "timeout")

		var x_position = rand_range(-10, 4)
		var y_position = rand_range(-7, 4)
		#position = Vector2(x_position, y_position) # greyed out because I don't want a jump to the new position


		var tween = Tween.new()
		add_child(tween)

		tween.interpolate_property(self, "position",
		x_position, y_position, 1,
		Tween.TRANS_EXPO, Tween.EASE_OUT)

		tween.start()
		tween.connect("tween_all_completed",tween,"queue_free")


		_ready()

However like that the Sprite doesn’t move at all (or does it, just to the same position?). Any idea what I’m missing here?

:bust_in_silhouette: Reply From: Wakatta

You have a type mismatch

    tween.interpolate_property(self, "position",
    position, Vector2(x_position, y_position), 1,
    Tween.TRANS_EXPO, Tween.EASE_OUT)

Dang, so close! Thanks again!

(I’d like to mention real quick how glad I am for all the help offered on this platform time and again. Such a great community, thank you!)

pferft | 2023-02-18 14:16