【问题标题】:Fading Ball in Roblox LuaRoblox Lua 中的褪色球
【发布时间】:2018-12-29 19:36:43
【问题描述】:

我正在尝试制作一个渐变到 1 透明度然后又回到 0 的球。 这是我的代码:

ball = script.Parent
trans = 0
while true do 
if trans < 1 then
    while trans < 1 do
        ball.Transparency = trans
        wait(0.1)
        trans = trans + 0.1 
    end
end
if trans == 1 then
    while trans <= 1 and trans >=0 do
        ball.Transparency = trans 
        wait(0.1)
        trans = trans -0.1 
    end
end
end

球确实消失了,但再也没有回来。此时游戏将冻结。有什么解决办法吗?谢谢!

更新:所以我今天尝试了以下代码,它工作正常,但是当我在 if 语句中将 if ball.Transparency == 1 替换为 trans == 1 时,会出现同样的问题。请解释一下谢谢!

while true do
ball = script.Parent
trans = 0
for i=0, 1, 0.1 do
    trans = i
    wait(0.1)
    ball.Transparency = trans
end
if ball.Transparency == 1 then
    for i = 1, 0, -0.1 do
        trans = i
        wait(0.1)
        ball.Transparency = trans
    end
end
end

【问题讨论】:

    标签: loops lua roblox


    【解决方案1】:

    浮点精度:

    0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1

    等于

    1

    我不确定为什么当您将 trans 值分配给 ball.Transparency 时,这不是一个因素。

    两件事:

    1) if tostring(trans) == “1.0” then 有效。

    2) 更好的是:如果 trans == 1,为什么还要检查那里?当然会,因为之前的 for 循环保证了这一点。

    另外,请谨慎使用while true...这可能是您的程序“冻结”的原因。

    【讨论】:

    • 感谢您的回答!我使用 while true 是因为我想让它永远持续下去。我认为在它们之间使用 wait 会很好。
    • 我将 if ball.Transparency 全部替换为 if true 并且效果很好。 :D
    猜你喜欢
    • 2016-05-13
    • 1970-01-01
    • 2022-06-17
    • 2023-02-02
    • 2021-06-25
    • 2021-09-03
    • 2022-10-20
    • 2020-07-02
    • 2021-10-28
    相关资源
    最近更新 更多