Home > Blockchain >  How to loop tweens without repeating similar code segments?
How to loop tweens without repeating similar code segments?

Time:12-14

I'm oscillating my character between 2 positions as such:

Idle -> Run & move right -> Idle -> Run & move left -> (Repeat)

extends Sprite

func _ready():
    var tween = get_tree().create_tween().set_loops()

    ## Idle 1 ##
    tween.tween_callback(animation_player,"play",["Idle"]) # plays idle animation 
    tween.tween_interval(2)
    ##

    ## Running 1 ##
    tween.tween_callback(animation_player,"play",["Running"]) # starts running animation
    tween.tween_property(self,"flip_h", false, 0)
    tween.tween_property(self,"position:x", 500.0, 2) # move position to 1000
    ##
    
    ## Idle 2 ##
    tween.tween_callback(animation_player,"play",["Idle"]) # plays idle animation 
    tween.tween_interval(2)
    ##
    
    ## Running 2 ##
    tween.tween_callback(animation_player,"play",["Running"]) # starts running animation
    tween.tween_property(self,"flip_h", true, 0)
    tween.tween_property(self,"position:x", -500.0, 2) # move position to 1000
    ##

and it works fine but the issue is that I have to write the Idle & Run Segment twice, which is really annoying

I tried this:

func _ready():
    var tween = get_tree().create_tween().set_loops()

    ## Idle ##
    tween.tween_callback(animation_player,"play",["Idle"]) # plays idle animation 
    tween.tween_interval(2) # pause for 2 seconds
    ##

    ## Running ##
    tween.tween_callback(animation_player,"play",["Running"]) # starts running animation
    tween.tween_property(self,"flip_h", !flip_h, 0)
    tween.tween_property(self,"position:x", position.x*-1, 2) # move position to 1000
    ##

but it seems that the literal value of the variable gets passed instead of the new one each time the loop runs

Is there anything I'm missing? or is there no work-around for this?

CodePudding user response:

This is working as intended. When you create the tweens the values are recorded. So that !flip_h is recorded, and that position.x*-1 is recorded.

As per workaround… This is what I have been able to come up with:

var flip_position:float

var advancement:float setget set_advancement
func set_advancement(mod_value:float) -> void:
    advancement = mod_value
    position.x = flip_position   mod_value * (-1.0 if flip_h else 1.0)

func do_flip() -> void:
    flip_h = !flip_h
    flip_position = position.x
    advancement = 0.0


func _ready() -> void:
    var tween := get_tree().create_tween().set_loops()

    ## Idle ##
    tween.tween_callback(animation_player,"play",["Idle"])
    tween.tween_interval(2)
    ##

    ## Running ##
    tween.tween_callback(animation_player,"play",["Running"])
    tween.tween_callback(self,"do_flip")
    tween.tween_property(self,"advancement", 500.0, 2)
    ##

Here I'm using a do_flip method so flip_h can change value. Furthermore, I'm using flip_h to know what direction it should run, and in flip_h I store from where it should run. Then I can make an advancement property which moves the character from the recorded position.

CodePudding user response:

I found this solution but it seems like a hack, please let me know if you have any proper method

extends Sprite

var running_direction=0 # 0=Right, 1=Left
var running_location:float=400

func toggle_run_direction():
    running_location*=-1
    
    if(running_direction): # left
        self.flip_h=true
        running_direction=0
    else: # right
        self.flip_h=false
        running_direction=1

# looping animations 
func loop_run_demo():
    
    var tween = get_tree().create_tween()
    
    ## Idle ##
    tween.tween_callback(animation_player,"play",["Idle"]) # plays idle animation 
    tween.tween_interval(2) # pause for 2 seconds
    ##
    
    tween.tween_callback(self,"toggle_run_direction")
    
    ## Running ##
    tween.tween_callback(animation_player,"play",["Running"])
    tween.tween_property(self,"position:x", running_location, 1.5)
    ##
    
    tween.connect("finished",self,"loop_run_demo")


func _ready():
    loop_run_demo()
  • Related