Swf 节点 API 参考¶
继承: Node2D
Swf 节点用于加载和播放 SWF 动画文件。
方法¶
| 方法 | 返回 | 说明 |
|---|---|---|
play_name(name: StringName) |
void | 通过 SymbolClass 名称播放动画 |
play_id(id: int) |
void | 通过角色 ID 播放动画 |
stop() |
void | 停止播放,保留在当前帧 |
resume() |
void | 从当前帧恢复播放 |
reset() |
void | 彻底停止,清空画面并回到第 0 帧 |
is_playing() const |
bool | 动画是否正在播放 |
get_current_frame() const |
int | 当前帧索引 |
get_frame_count() const |
int | 当前动画总帧数 |
goto_frame(frame: int, stop: bool = false, play_children: bool = false) |
void | 跳转到指定帧 |
goto_label(label: String, stop: bool = false, play_children: bool = false) |
void | 按帧标签跳转 |
render_frame() |
void | 手动触发一帧渲染 |
get_display_object(path: String) const |
Object | 按路径查找显示对象(暂未实现) |
goto_frame / goto_label 参数说明¶
- stop — 跳转后停止在该帧
- play_children — 当 stop 为 true 时,父元件停止但子元件继续播放。stop 为 false 时此参数无效
属性¶
| 属性 | 类型 | 默认值 | 说明 |
|---|---|---|---|
swf_resource |
SwfResource | — | 关联的 SWF 资源 |
animation_id |
int | 0 | 当前选中的角色 ID(编辑器属性,运行时用 play_id()) |
animation_name |
String | "" | 当前选中的动画名称,编辑器下拉枚举(编辑器属性,运行时用 play_name()) |
preview_enabled |
bool | true | 是否在编辑器中预览(仅编辑器生效) |
auto_play |
bool | false | 运行时在 NOTIFICATION_READY 时自动播放(编辑器设置属性,运行时用函数控制) |
preview_frame |
int | -1 | 编辑器预览帧索引(-1 实时播放) |
playing |
bool | false | 只读。动画是否正在播放 |
loop |
bool | true | 是否循环播放 |
speed_scale |
float | 1.0 | 播放速度倍率 |
current_frame |
int | 0 | 只读。当前帧索引 |
frame_count |
int | 0 | 只读。当前动画总帧数 |
render_target_size |
Vector2i | (512, 512) | 渲染目标尺寸 |
root_position |
Vector2 | (0, 0) | 根实例位置 |
root_rotation |
float | 0.0 | 根实例旋转(弧度) |
root_scale |
Vector2 | (1, 1) | 根实例缩放 |
root_skew |
float | 0.0 | 根实例倾斜(弧度) |
信号¶
animation_finished()¶
当前动画播放完成时触发(非循环模式)。
animation_looped()¶
循环动画播放完一轮时触发。
代码示例¶
extends Node2D
@onready var swf = $Swf
func _ready():
# 加载资源
swf.swf_resource = preload("res://assets/hero.swf")
# 运行时用函数控制播放,不用编辑器属性
swf.play_name("idle")
swf.loop = true
# 信号
swf.animation_finished.connect(_on_anim_finished)
swf.animation_looped.connect(_on_anim_looped)
func _input(event):
if event.is_action_pressed("ui_accept"):
if swf.is_playing():
swf.stop()
else:
swf.resume()
func _on_anim_finished():
print("动画播放完成")
func _on_anim_looped():
print("循环一轮")