首页 > 解决方案 > 我希望 Roblox Studio 上的文本按钮颜色淡入淡出,但出现错误

问题描述

我使用 Lua Roblox,我试图让文本按钮从蓝色变为橙色淡出和淡入,我的脚本出错了,我该怎么办?

代码:

game.StartedGui.ScreenGui.TextButton.Color = ””—- I typed orange color inside these brackets —-
wait(.5)
game.StartedGui.ScreenGui.TextButton.Color = “”—- I typed blue color inside these brackets—-

它没有褪色,颜色仍然是白色。另外,我如何循环播放,因为我不希望颜色停止变化。

标签: luaroblox

解决方案


如果我的理解正确,您想让按钮背景颜色从蓝色变为橙色并无限循环吗?尝试将以下 LocalScript 放在 TextButton 下方。

local textButton = script.Parent

local color1 = Color3.fromRGB(128, 128, 255)    -- blue

local color2 = Color3.fromRGB(218, 133, 65)     -- orange

local changeSpeed = 10      -- increase this to make slower 



spawn(function()
    local i = -1
    while true do
        for i=-1,1,1/changeSpeed do
            local f = math.abs(i)
            textButton.BackgroundColor3 = Color3.fromRGB(
                255 * (color1.r + (color2.r - color1.r) * f), 
                255 * (color1.g + (color2.g - color1.g) * f),
                255 * (color1.b + (color2.b - color1.b) * f)
            )
            wait(0.05)                          
        end
    end
end) 

更新:

...或者使用非常酷的 Tween 服务,正如 Kylaaa 所提到的:

[...]
local changeSpeed = 1      -- increase this to make slower 

textButton.BackgroundColor3 = color1
local tw = game.TweenService:Create(textButton, TweenInfo.new(changeSpeed, Enum.EasingStyle.Sine, Enum.EasingDirection.InOut, -1, true), { BackgroundColor3 = color2 })
tw:Play()

推荐阅读