温馨提示:本文翻译自stackoverflow.com,查看原文请点击:animation - Why am I getting this error, when character is fully loaded?
animation lua member roblox

animation - 字符完全加载后,为什么会出现此错误?

发布于 2020-04-08 11:12:25

我正在尝试第一次将动画插入代码中。这是我的代码:

1   local player = game.Players.LocalPlayer
2   local char = player.Character or player.CharacterAdded:Wait()
3   local hum = char:WaitForChild("Humanoid")
4    
5   local animaInstance = Instance.new("Animation")
6   animaInstance.AnimationId = "rbxassetid://4641537766"
7    
8   local fireBallAnim = hum.LoadAnimation(animaInstance)
9   fireBallAnim:Play()

我收到错误

The function LoadAnimation is not a member of Animation

我知道角色已满载,所以我听不懂。如果动画本身有问题,是否会出现此错误?我还能错过什么?

谢谢

查看更多

提问者
Paul
被浏览
60
Kylaaa 2020-02-01 14:31
local fireBallAnim = hum:LoadAnimation(animaInstance)

这是一个非常令人困惑的错误消息。唯一的问题是,您使用.而不是调用了成员函数:将其切换为冒号将解决您的错误。


在带有冒号的对象上调用函数时,它会自动将对象插入为第一个参数。用表可以看到一个有趣的例子:

-- insert an object into the table 't'
local t = {}
table.insert(t, 1)
-- can also be written as...
t.insert(t, 1)
-- which is the same as...
t:insert(1)

所有这些调用都执行相同的操作。使用调用该函数:是将t对象作为第一个参数的语法糖所以在您的代码中,正在发生的事情是您像这样调用Lo​​adAnimation:

local fireBallAnim = hum.LoadAnimation(<a humanoid object needs to go here>, <animation>)

但是,由于您要传递人形机器人应该到达的动画,因此它想在动画对象上找到LoadAnimation函数,但失败了。