在Roblox Lua中实现淡出的球

我正在尝试制作一个能够从完全不透明淡出到1透明度再恢复到完全不透明的球。 以下是我的代码:

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 语句中的 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
点赞
用户4403144
用户4403144

浮点数准确度:

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)如果'class="highlight">tostring(trans) ==“1.0”then`是起作用的。

2)更好的方法是:为什么要检查trans == 1?当然会,因为就在那之前的for循环保证了它。

此外,小心使用'while true'......这可能是你的程序“冻结”的原因。

2018-07-22 15:30:58