1

我正在为魔兽世界创建一个附加组件。

我有这个:

if edirection == "moon" then sffem = 105*math.sin(math.pi - math.asin(cpower/105) + math.pi/20 * sfcasttime) end

这工作正常,但我需要将截止点设置为 100 和 -100。

这是因为我的角色的能量是基于一个正弦波,从 0 开始下降到 -100 在那里停留几秒钟,然后回到 0 上升到 100 并停留几秒钟然后回到 0。

这是有效的,因为正弦波适用于 105、-105 能量,但玩家的最大和最小能量为 100。

我试过了:

if edirection == "moon" then sffem = (MAX(-100;MIN(100;105*math.sin(math.pi - math.asin(cpower/105) + math.pi/20 * sfcasttime)))) end

这只是给出一个错误。

我怎样才能做到这一点?

4

2 回答 2

3

无需在一行中完成所有这些操作。例如,在该行之后

if edirection == "moon" then sffem = 105*math.sin(math.pi - math.asin(cpower/105) + math.pi/20 * sfcasttime) end

做类似的事情

if sffem >= 100 then sffem = 100 end
if sffem <= -100 then sffem = -100 end

(感谢 Henrik Ilgen 的语法帮助)

于 2015-08-21T12:20:12.423 回答
0

您的第二行代码使用分号而不是逗号来分隔MAXand的参数MIN

更改并使用math.minand之后的代码math.max

if edirection == "moon" then sffem = math.max(-100,math.min(100,105*math.sin(math.pi - math.asin(cpower/105) + math.pi/20 * sfcasttime))) end

您可能会发现创建一个钳位辅助函数很有用:

function clamp(value, min, max)
  return math.max(min, math.min(max, value))
end

在这种情况下,您的代码将变为:

if edirection == "moon" then sffem = clamp(105*math.sin(math.pi - math.asin(cpower/105) + math.pi/20 * sfcasttime), -100, 100) end
于 2015-08-21T21:46:01.490 回答