我目前正在尝试动态创建动画并播放它以根据文本的长度和每秒字符的设定速度来显示文本。
我试图在代码中重新创建的是这个动画:
所以在标签的属性上有一个属性轨道的动画,更新模式是连续的,插值模式是线性的
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 Engine v3.1.2.stable.mono.official - https://godotengine.org
OpenGL ES 3.0 渲染器:AMD Radeon R7 200 系列
标签文本:Magnam consequatur vel alias 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
正在播放:真正
的当前动画:显示文本
** 调试过程已停止 **