【问题标题】:Looping from one value to another over a span of time在一段时间内从一个值循环到另一个值
【发布时间】:2014-06-26 14:09:47
【问题描述】:

我目前正在为 Roblox 的 PointLight 对象编写方法。目前,我正在编写方法来增加和减少 PointLight 的 Brightness 属性,但我已经靠在墙上试图获得我需要的公式。

程序需要在 DarkenSpeed(2 秒)的范围内从 Brightness (1) 循环到 FadedBrightness (.3)。我已经尝试在谷歌上搜索可能的解决方案,但我担心我正在寻找的内容过于具体。

这是一个代码sn-p:

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

如果有任何更好的方法来实现这一点 - 甚至是不同的方法 - 我会全力以赴。我倾向于用我的代码获得隧道视野,除了当前的问题之外,我没有考虑任何其他事情。

【问题讨论】:

  • 您最大的问题是试图弄清楚如何在 2 秒内传播它?脚本环境中是否有任何类型的计时器/延迟功能?例如delayfunction( 500, myfunc) -- execute myfunc after 500ms?
  • 啊,我看到 wait() 是 Roblox 的收益函数。
  • 是的。让它持续达到 FadedBrightness 更多的是事后考虑,因为如果循环结束,我可以在循环之后设置 Brightness 属性——尽管这可能会导致一些视觉上的混乱。
  • 这个问题似乎是题外话,因为它是关于Code Review
  • @hjpotter92 哎呀!对于那个很抱歉。我会看到有关编辑帖子或在那里重新发布的信息。感谢您的提醒。

标签: timer lua roblox


【解决方案1】:

根据 Roblox 文档,wait() 在没有参数的情况下产生大约 1/30 秒的时间: http://wiki.roblox.com/index.php?title=Function_dump/Functions_specific_to_ROBLOX#Wait

您可以尝试使用 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

【讨论】:

  • 稍后我会试一试。需要注意的是,Roblox 限制了一些东西,os 中仅有的两种方法是time()difftime(),这会是个问题吗?
  • wait 实际上会返回经过的时间和地点时间(看起来与os.clock() 相同),因此您可以尝试使用它。上面编辑说明没有os.clock()
  • 我成功了!太感谢了!没有你提供的东西,我做不到。我应该用我的代码更改来编辑你的答案还是我自己做答案?这主要是你的代码,所以我假设我应该编辑。
【解决方案2】:

从 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

【讨论】:

    猜你喜欢
    • 2015-12-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多