You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Godot C#中Animation无TrackSetLoop方法的报错问题求助

问题解决思路

1. 直接报错原因:不存在TrackSetLoop方法

Godot的Animation类没有TrackSetLoop这个方法,你错误地认为轨道可以单独设置循环,但循环是动画本身的属性,不是轨道的。要让动画循环,直接设置动画的Loop属性即可:

// 替换掉 animation.TrackSetLoop(trackIndex, true);
animation.Loop = true;
// 如果需要来回切换高亮(正常<->高亮),可以设置循环模式为PingPong
animation.LoopMode = Animation.LoopModeEnum.PingPong;

2. 修复AnimationPlayer添加动画的错误

你用了_animationPlayer.AddAnimationTrack(animationName, animation);,这是错误的方法。AnimationPlayer添加完整动画应该用AddAnimation:

// 替换错误的AddAnimationTrack
_animationPlayer.AddAnimation(animationName, animation);

3. 修复Tab按钮获取的逻辑错误

你传入的scenePath是场景文件路径(比如res://Scenes/One.tscn),但GetNode只能获取当前场景树中的节点路径,不能直接加载场景文件里的节点。正确的做法是:

  • 把CreateHighlightAnimation的第二个参数改成TabBar节点下的TextureButton的节点路径(比如"StoreButton")
  • 或者如果按钮是当前Tab节点的子节点,直接用相对路径获取:
// 修改CreateHighlightAnimation方法的参数和获取逻辑
private void CreateHighlightAnimation(string animationName, string buttonNodePath)
{
    var animation = new Animation();
    animation.Length = 0.5f;
    animation.Loop = true;
    animation.LoopMode = Animation.LoopModeEnum.PingPong;

    // 获取当前Tab节点下的TextureButton
    var tabButton = GetNode<TextureButton>(buttonNodePath);
    var tabPath = tabButton.GetPath();

    var trackIndex = animation.AddTrack(Animation.TrackType.Value);
    animation.TrackSetPath(trackIndex, tabPath + ":modulate");
    animation.TrackInsertKey(trackIndex, 0.0f, new Color(1, 1, 1, 1));
    animation.TrackInsertKey(trackIndex, 0.5f, new Color(0.8f, 0.8f, 0.8f, 1));

    _animationPlayer.AddAnimation(animationName, animation);
}

// 在_Ready里调用时传入按钮节点路径,比如假设按钮是Tab的子节点,命名为StoreBtn、SettingsBtn等
public override void _Ready()
{
    _animationPlayer = GetNode<AnimationPlayer>("TabBarAnimationPlayer");

    CreateHighlightAnimation("HighlightStore", "StoreBtn");
    CreateHighlightAnimation("HighlightSettings", "SettingsBtn");
    CreateHighlightAnimation("HighlightHome", "HomeBtn");
    CreateHighlightAnimation("HighlightLeader", "LeaderBtn");
}

4. 额外优化建议

  • 切换场景时,建议先停止其他动画,避免多个高亮动画同时播放:
private void _on_texture_button_pressed()
{
    _animationPlayer.StopAll();
    GetTree().ChangeSceneToFile("res://Scenes/One.tscn");
    _animationPlayer.Play("HighlightStore");
}
  • 如果只是需要简单的高亮切换,也可以用Tween节点来实现,代码会更简洁,无需手动创建Animation轨道。

内容的提问来源于stack exchange,提问作者SpaceDevs

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.03 19:28:40