Skip to main content
Skip to main content

Tween

Tween

Description

Tween class which linearly interpolates a quantity from a start value to an end value over a given duration.

Functions

applyValue

Description

Apply a value via the setter function.

Definition

applyValue()

Arguments

anynewValue

Code

function Tween:applyValue(newValue)
if self.functionTarget ~ = nil then
self.setter( self.functionTarget, newValue)
else
self.setter(newValue)
end
end

getDuration

Description

Get this tween's duration in milliseconds.

Definition

getDuration()

Code

function Tween:getDuration()
return self.duration
end

getFinished

Description

Check if this tween has finished.

Definition

getFinished()

Code

function Tween:getFinished()
return self.isFinished
end

new

Description

Create a new Tween.

Definition

new(function setterFunction, float startValue, float endValue, float duration, table? customMt)

Arguments

functionsetterFunctionValue setter function. Signature: callback(value) or callback(target, value).
floatstartValueOriginal value
floatendValueTarget value
floatdurationDuration of tween in milliseconds
table?customMtSubclass metatable for inheritance

Code

function Tween.new(setterFunction, startValue, endValue, duration, customMt)
local self = setmetatable( { } , customMt or Tween _mt)

self.setter = setterFunction
self.startValue = startValue
self.endValue = endValue
self.duration = duration
self.elapsedTime = 0

self.isFinished = duration = = 0
self.functionTarget = nil

self.curveFunc = Tween.CURVE.LINEAR

return self
end

reset

Description

Reset this tween to play it again.

Definition

reset()

Code

function Tween:reset()
self.elapsedTime = 0
self.isFinished = self.duration = = 0
end

setCurve

Description

Set the curve function. Defaults to Tween.CURVE.LINEAR

Definition

setCurve()

Arguments

anyfunc

Code

function Tween:setCurve(func)
self.curveFunc = func or Tween.CURVE.LINEAR
end

setTarget

Description

Set a callback target for this tween. If a target has been set, the setter function must support receiving the target as its first argument.

Definition

setTarget()

Arguments

anytarget

Code

function Tween:setTarget(target)
self.functionTarget = target
end

tweenValue

Description

Get the current tween value.

Definition

tweenValue()

Arguments

anyt

Code

function Tween:tweenValue(t)
return MathUtil.lerp( self.startValue, self.endValue, self.curveFunc(t))
end

update

Description

Update the tween's state.

Definition

update()

Arguments

anydt

Code

function Tween:update(dt)
if self.isFinished then
return
end

self.elapsedTime = self.elapsedTime + dt

local newValue
if self.elapsedTime > = self.duration then
self.isFinished = true
newValue = self:tweenValue( 1 )
else
local t = self.elapsedTime / self.duration
newValue = self:tweenValue(t)
end

self:applyValue(newValue)
end