0 votes

Hi all,

I have a sprite (a man in this case) and I want it to scale up and down just once to draw the user's eye to it.
I tried this:

extends Node2D

onready var man = get_node("Sprite")
onready var tween = get_node("Tween")


func _ready():
    growShrinkSprite(man, tween, 2, .5)


func growShrinkSprite(spriteName, tweenName, scaleUp, seconds):

    #scale up
    tweenName.interpolate_property(spriteName, "transform/scale", Vector2(0,0), Vector2(scaleUp,scaleUp), seconds, Tween.TRANS_LINEAR, Tween.EASE_IN_OUT)
    tweenName.start()

... and it works perfectly to scale UP, but I am unsure now of how to make it scale back down again.

I tried placing another interpolate straight after like so:

tweenName.interpolate_property(spriteName, "transform/scale", Vector2(0,0), Vector2(scaleUp,scaleUp), seconds, Tween.TRANS_LINEAR, Tween.EASE_IN_OUT)
tweenName.interpolate_property(spriteName, "transform/scale", Vector2(scaleUp,scaleUp), Vector2(0,0), seconds, Tween.TRANS_LINEAR, Tween.EASE_IN_OUT)
tweenName.start()

... but that only triggers the last one. So it scales from full size down to 0. Not both, one after the other like I'd hoped.

I've played with a range of variations but can't crack it.

Can anyone shed some light to help out a (insert sob story here)? :-)

in Engine by (810 points)

1 Answer

+1 vote

OK I figured out a working example.
I'm happy to use it but it feels a little... cludgy.

If anyone has any suggestions for improvement just shout otherwise here is my solution for anyone in the future:

extends Node2D

onready var man     = get_node("Sprite")#the sprite
onready var tween   = get_node("Tween") #the tween
var direction       = "down"            #the initial direction
var shrinkGrowTime  = .5

func _ready():
    growShrinkSprite(man, tween, 2, 0, shrinkGrowTime)  #start the process off

func growShrinkSprite(spriteName, tweenName, scaleDown, scaleUp, seconds):
    tweenName.interpolate_property(spriteName, "transform/scale", Vector2(scaleDown,scaleDown), Vector2(scaleUp,scaleUp), seconds, Tween.TRANS_LINEAR, Tween.EASE_IN_OUT)
    tweenName.start()

#once the tween has complete its execution and finished, start it off again, this time reversed
func _on_Tween_tween_complete( object, key ):
    if direction == "down":
        growShrinkSprite(man, tween, 0, 2, shrinkGrowTime)
        direction = "up"
    else:
        growShrinkSprite(man, tween, 2, 0, shrinkGrowTime)
        direction = "down"
by (810 points)

For current version of Godot replace "transform/scale" with "scale".

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.

Categories