lua - 我希望 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—-
它没有褪色,颜色仍然是白色。另外,我如何循环播放,因为我不希望颜色停止变化。
解决方案
如果我的理解正确,您想让按钮背景颜色从蓝色变为橙色并无限循环吗?尝试将以下 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()
推荐阅读
- java - java.lang.IllegalStateException:无法执行 android:onClick / Android 的方法
- mongodb - mongodb 更改流无法解析订阅
- google-sheets - 如何分配增量数字,但前提是另一列是不同的值
- ios - iOS 上的数据共享
- java - 单击按钮时,JavaFX 在新线程上运行任务
- c - 传递给 C 中的函数之前和之后的 sizeof(array) 之间的差异
- python - 尝试下载较大的压缩 csv 文件时 Python 代码中断,在较小的文件上工作正常
- excel - 不可能的 Excel-VBA 电子邮件循环
- java - 在使用 Maven 编译的胖 jar 中时出现 Apache POI ClasDefNotFoundException
- service-worker - 重试失败的动态导入?(SPA/PWA)