【问题标题】:Dynamically create animation for showing text动态创建动画以显示文本
【发布时间】:2019-12-02 04:45:29
【问题描述】:

我目前正在尝试动态创建动画并播放它以根据文本的长度和每秒字符的设定速度来显示文本。

我试图在代码中重新创建的是这个动画:

因此,在标签属性visible_characters 上具有属性轨迹的动画,更新模式为连续,插值模式为线性

场景结构为:

DialogBox 节点后面的脚本:

extends Control

export(String, MULTILINE) var Text = ""
export(int) var CharactersPerSecond = 100

func _ready():
    $Panel/Label.set_text(Text)
    print($Panel/Label.get_text())
    createAnimation()
    $AnimationPlayer.play("show-text")
    print("is playing " + str($AnimationPlayer.is_playing()))
    print("current animation " + $AnimationPlayer.current_animation)

func createAnimation():
    var animationLength = Text.length() / (CharactersPerSecond as float)

    print(animationLength)

    var animation = $AnimationPlayer.get_animation("show-text")
    animation.clear()

    var trackIdx = animation.add_track(Animation.TYPE_VALUE)
    animation.track_set_path(trackIdx, "Panel/Label:visible_characters")
    animation.track_set_interpolation_type(trackIdx,Animation.INTERPOLATION_LINEAR)
    animation.value_track_set_update_mode(trackIdx, Animation.UPDATE_CONTINUOUS)

    animation.track_insert_key(trackIdx, 0, 0)
    animation.track_insert_key(trackIdx, animationLength, Text.length())

出于测试目的,正在使用导出的Text 变量在编辑器中设置文本,并且是一些 lorem ipsum。

当我运行场景时会发生什么情况是面板和标签显示,但标签中没有显示任何文本,它保持为空,但根据打印语句 show-text 动画正在播放

输出窗口中打印出来的数据是:

** 调试过程已启动 **
Godot 引擎 v3.1.2.stable.mono.official - https://godotengine.org
OpenGL ES 3.0 渲染器:AMD Radeon R7 200 系列
标签文本:Magnam consequatur vel 别名 earum accusantium。 Nobis voluptatem voluptatem quaerat adipisci voluptas。 Numquam id error earum consectetur veniam。 Quaerat quibusdam quas sunt alias et blanditiis corporis。 Cupidate rem ut natus est molestiae quidem。 Magnam consequatur vel 别名 earum accusantium。 Nobis voluptatem voluptatem quaerat adipisci voluptas。 Numquam id error earum consectetur veniam。 Quaerat quibusdam quas sunt alias et blanditiis
动画长度:4.44
正在播放:真
当前动画:显示文本
** 调试过程停止 **

【问题讨论】:

    标签: godot gdscript


    【解决方案1】:

    所以问题是我仍然需要设置动画的长度
    仅仅插入钥匙是不够的

    在animation.clear() 之后添加以下行修复它:

    animation.set_length(animationLength)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-05-01
      • 1970-01-01
      • 2016-01-12
      • 2017-11-03
      • 1970-01-01
      • 2016-04-21
      • 2015-09-09
      • 1970-01-01
      相关资源
      最近更新 更多