How to use tween with the region_rect property

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By ludosar
:warning: Old Version Published before Godot 3 was released.

Hi everybody,

I’m trying to animate the region_rect property of a sprite with a tween node, and so far, it doesn’t work.

I was able to animate other properties, but not this one, maybe because it uses a Rect2 var?

this is what I use:

var sprite = get_node(“Sprite”)
var current_coordinates = sprite.get_region_rect()
var target_coordinates = current_coordinates
target_coordinates.pos.x += 280
tween.interpolate_property(sprite, “region_rect”, current_coordinates, target_coordinates, 1.5, Tween.TRANS_EXPO, Tween.EASE_OUT, 0)

:bust_in_silhouette: Reply From: Zylann

A region rect cannot be interpolated. In cases like this, you can use interpolate_method() instead and send it the start and stop positions of the rectangle:

tween.interpolate_method(self, "_tween_region_rect", current_coordinates, target_coordinates, 1.5, Tween.TRANS_EXPO, Tween.EASE_OUT, 0)

func _tween_region_rect(pos):
	var rect = sprite.get_region_rect()
	rect.pos = pos
	sprite.set_region_rect(rect)

Thank you very much, it works perfectly!
(just had to add the missing _ in TRANS_EXPO)

ludosar | 2016-07-13 11:07