0 votes

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 = getnode("Sprite")
var current
coordinates = sprite.getregionrect()
var targetcoordinates = currentcoordinates
targetcoordinates.pos.x += 280
tween.interpolate
property(sprite, "regionrect", currentcoordinates, targetcoordinates, 1.5, Tween.TRANSEXPO, Tween.EASE_OUT, 0)

in Engine by (281 points)

1 Answer

+2 votes
Best answer

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)
by (28,778 points)
edited by

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

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.