关于 TweenInfo 中提示“TweenInfo.new first argument expects a number for time”的错误信息

我正在制作一个 Tween,但是我收到一个错误信息,说“TweenInfo.new first argument expects a number for time”,这是什么问题?

local tweenInfo = TweenInfo.new{
    0.75,
    Enum.EasingStyle.Sine,
    Enum.EasingDirection.Out,
    0,
    false,
    0
}
-- later on when I call it
tweenService:Create(v, tweenInfo, Vector3.new(X,Y,Z)) -- v is an Instance of a Part

请帮助我!

点赞
用户2860267
用户2860267

你遇到了 Lua 语言的一个有趣特性。如果只有一个参数提供,函数调用中括号是可选的

所以实际上在你的代码中发生的是这样的...

local tweenInfo = TweenInfo.new(
    { 参数表 }, -- 时间
    nil, -- 缓动样式
    nil, -- 缓动方向
    nil, -- 重复计数
    nil, -- 是否反向
    nil -- 延迟
)

TweenInfo 构造函数期望第一个参数是一个数字,但实际上传入的是一个值表。

所以,只需要将花括号替换为括号即可修复:

local tweenInfo = TweenInfo.new(
    0.75,
    Enum.EasingStyle.Sine,
    Enum.EasingDirection.Out,
    0,
    false,
    0
)

--编辑:根据评论,TweenService:Create() 中也存在语法错误。以下代码是修复后的版本:

tweenService:Create(v, tweenInfo, {
    Position = Vector3.new(X,Y,Z),
})
2020-10-14 05:15:46