Timer 在一段时间内从一个值循环到另一个值

Timer 在一段时间内从一个值循环到另一个值,timer,lua,roblox,Timer,Lua,Roblox,我目前正在为Roblox的对象编写方法。目前,我正在编写一些方法来增加和减少PointLight的亮度,但是我一直在努力得到我需要的公式 程序需要在变暗速度(2秒)的范围内从亮度(1)循环到衰减亮度(.3)。我试着在谷歌上搜索一个可能的解决方案,但我担心我要找的太具体了 下面是一段代码片段: local LightSource = { -- The value the PointLight's Brightness property will be when night and day,

我目前正在为Roblox的对象编写方法。目前,我正在编写一些方法来增加和减少PointLight的亮度,但是我一直在努力得到我需要的公式

程序需要在变暗速度(2秒)的范围内从亮度(1)循环到衰减亮度(.3)。我试着在谷歌上搜索一个可能的解决方案,但我担心我要找的太具体了

下面是一段代码片段:

local LightSource = {
  -- The value the PointLight's Brightness property will be when night and day,
  -- respectively.
  Brightness = 1,
  FadedBrightness = .3,

  -- How long (in seconds) it takes for the light source's brightness to be
  -- faded in/out.
  BrightenSpeed = 2,
  DarkenSpeed = 2
}

-- There is an IncreaseBrightness method, but that should be easy enough to
-- modify once the below is working.

function LightSource:DecreaseBrightness()
  -- self.Light refers to the PointLight instance.
  -- light.Brightness would be the current `Brightness` property.
  local light = self.Light

  -- Need to combine Brightness, FadedBrightness and DarkenSpeed into a formula
  -- for decrementing.

  local decrement = self.FadedBrightness / (self.Brightness * self.DarkenSpeed) -- 0.15, that won't work at all.

  while light.Brightness >= self.FadedBrightness do
    light.Brightness = light.Brightness - decrement
    wait()
  end
end

如果有更好的方法来实现这一点——或者甚至有不同的方法——我洗耳恭听。我倾向于用我的代码获得隧道式的视觉效果,除了目前的问题,我什么都不考虑。

根据Roblox doc的说法,wait()可能会在没有任何参数的情况下产生1/30秒的收益:

您可以尝试使用os.clock()来计算每个循环的差异,并按比例缩放。 未经测试的代码,但应该给出一个想法:

function LightSource:DecreaseBrightness()
  -- self.Light refers to the PointLight instance.
  -- light.Brightness would be the current `Brightness` property.
  local light = self.Light

  -- Need to combine Brightness, FadedBrightness and DarkenSpeed into a formula
  -- for decrementing.
  local strtBright=light.Brightness
  local _,strtTime=wait()
  local finTime=strt+self.DarkenSpeed
  while light.Brightness >= self.FadedBrightness do
    local _,currTime=wait()
    light.Brightness = math.max( self.FadedBrightness, strtBright - (strtBright-self.FadedBrightness) * ((curr-strtTime)/(finTime-strtTime)) )
  end
end

从Odoth的示例代码中,我将
wait()
切换为
tick()
,更改变量名称以匹配我的编码风格,并将公式分成两行以便于阅读

它工作得很好,唯一的问题是它不会在FadedBrightness(超过0.298)时完全停止,所以我在方法的底部添加了一行来抵消不准确

时间安排非常好,我对结果非常满意

完成方法:

function LightSource:DecreaseBrightness()
  local light = self.Light
  local startBrightness = light.Brightness
  local startTime = tick()
  local endTime = startTime + self.DarkenSpeed

  while light.Brightness >= self.FadedBrightness do
    local currentTime = tick()
    local timer = (currentTime - startTime) / (endTime - startTime)
    local brightness = startBrightness - (startBrightness - self.FadedBrightness) * timer

    light.Brightness = brightness
    wait()
  end

  -- Set the brightness in case the loop spills over.
  light.Brightness = self.FadedBrightness
end

你最大的问题是想知道如何在2秒内传播它?脚本环境中是否公开了任何类型的计时器/延迟功能?e、 g.
delayfunction(500,myfunc)--在500毫秒后执行myfunc
?啊,我看到wait()是一个Roblox屈服函数。是的。让它持续地点击FadedBrightness更多的是事后考虑,因为我可以在循环结束后设置
亮度
属性,尽管这可能会引起一点视觉冲击。这个问题似乎脱离主题,因为它是关于@hjpotter92哇!很抱歉。我会考虑编辑帖子或在那里重新发布。谢谢你提醒我,我稍后再试试。需要注意的是,Roblox限制了一些东西,
os
中仅有的两种方法是
time()
difftime()
,这会有问题吗?
wait
实际上返回经过的时间和地点时间(看起来与
os.clock()
相同),所以您可以尝试使用它。在上面编辑以说明没有
os.clock()
。我让它工作了!非常感谢你!没有你提供的东西我是做不到的。我应该用我的代码修改来编辑你的答案,还是我自己做答案?这主要是你的代码,所以我想我应该编辑。