我正在尝试第一次将动画插入代码中。这是我的代码:
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
我知道角色已满载,所以我听不懂。如果动画本身有问题,是否会出现此错误?我还能错过什么?
谢谢
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
对象作为第一个参数的语法糖。所以在您的代码中,正在发生的事情是您像这样调用LoadAnimation:
local fireBallAnim = hum.LoadAnimation(<a humanoid object needs to go here>, <animation>)
但是,由于您要传递人形机器人应该到达的动画,因此它想在动画对象上找到LoadAnimation函数,但失败了。