【问题标题】:How to loop tweens without repeating similar code segments?如何在不重复相似代码段的情况下循环补间?
【发布时间】:2022-12-14 11:26:25
【问题描述】:

我在 2 个位置之间摇摆我的角色:

空闲 -> 运行并向右移动 -> 空闲 -> 运行并向左移动 -> (重复)

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
    ##

它工作正常但问题是我必须写两次 Idle & Run 段,这真的很烦人

我试过这个:

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
    ##

但似乎每次循环运行时传递的是变量的字面值而不是新值

有什么我想念的吗?还是没有解决方法?

【问题讨论】:

    标签: animation godot gdscript tween


    【解决方案1】:

    这是按预期工作的。创建补间时,会记录这些值。这样就记录了!flip_h,记录了position.x*-1

    根据解决方法……这就是我能够想出的:

    var flip_position:float
    
    var advancement:float getset 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
    
    
    func _ready() -> void:
        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_callback(self,"do_flip")
        tween.tween_property(self,"advancement", 500, 2) # move position to 1000
        ##
    

    这里我使用了do_flip方法,所以flip_h可以改变值。此外,我使用 flip_h 来了解它应该运行的方向,而在 flip_h 中,我存储它应该运行的位置。然后我可以制作一个 advancement 属性,将角色从记录的位置移开。

    【讨论】:

      【解决方案2】:

      我找到了这个解决方案,但它似乎是一个 hack,如果你有任何合适的方法,请告诉我

      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()
      

      【讨论】:

      • 你秒杀我。
      • @Theraot 但你的回答更有效率,所以我们称之为平局 :)
      猜你喜欢
      • 1970-01-01
      • 2020-03-19
      • 1970-01-01
      • 2021-04-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多