Springer
- Wally Package: Springer
Springer is a class that allows you to animate values using a spring physics model.
The values you can animate are numbers, Vector2, and Vector3.
You can set the target value, frequency, and damping to customize the spring.
You can also listen to the onStep
SpringerSignal to get the current value of the spring.
When the spring reaches the target value, the onComplete
SpringerSignal will be fired.
Here is an example of how to use the Springer class to animate a number from 0 to 1:
local springer = Springer.new(0, 5, .2)
springer:SetTarget(1, 10, .8)
springer.onStep:Connect(function(value)
print(value)
end)
springer.onComplete:Connect(function()
print("Springer completed")
end)
Here is an example of how to use the Springer class to animate a Vector3 from (0, 10, 0) to (5, 15, 8) in 1 second:
local springer = Springer.new(Vector3.new(0, 10, 0), 5, .2)
springer:SetTarget(Vector3.new(5, 15, 8), 10, .8)
springer.onStep:Connect(function(value)
print(value)
end)
springer.onComplete:Connect(function()
print("Springer completed")
end)
The springer class when begin animating immediately on the next frame if you pass the initial goal value. Here is an example of how to use the Springer class to animate a number from 0 to 1 in 1 second immediately:
local springer = Springer.new(0, 5, .2, 1)
springer.onStep:Connect(function(value)
print(value)
end)
NOTE
- When you call the `SetTarget` method, the spring will start animating towards the target value.
- If you set any of the properties directly, the spring animation will override them.
- Setting isActive to false will stop the spring animation.(It will then not call the onComplete signal)
- Setting isActive to true will NOT start the spring animation. You need to call the SetTarget method to start the animation.
- Setting the properties directly will not animate the spring.
Types
Springer
interface
Springer {
frequency:
number
--
The frequency of the spring.
damping:
number
--
The damping of the spring.
springType:
string
--
The type of the spring value.
isActive:
boolean
--
Whether the spring is active or not.
}
SpringerSignal
interface
SpringerSignal {
}
Properties
onComplete
EventSpringer.onComplete:
SpringerSignal
Fired when the spring reaches the target value, and is no longer animating.
local springer = Springer.new(0, 5, .2)
springer:SetTarget(1, 10, .8)
springer.onComplete:Connect(function()
print("Springer completed")
end)
onStep
EventSpringer.onStep:
SpringerSignal
Fired every frame with the current value of the spring. (RenderStepped for client, Heartbeat for server)
local springer = Springer.new(0, 5, .2)
springer:SetTarget(1, 10, .8)
springer.onStep:Connect(function(value)
print(value)
end)
Functions
SetTarget
Springer.
SetTarget
(
frequency:
number?
,
--
The frequency of the spring.
damping:
number?
--
The damping of the spring.
) →
Springer
--
The Springer instance.
Springer:SetTarget(newTarget: number | Vector2 | Vector3, frequency: number?, damping: number?): Springer
Sets the target value of the spring. The spring will start animating towards the target value. You can also set the frequency and damping of the spring. If the frequency and damping are not provided, the spring will use the default values of 1.
Here is an example of how to use the SetTarget method to bounce between two values every 3 seconds:
local springer = Springer.new(0, 4, .3)
local switch = true
while true do
task.wait(3)
if switch then
springer:SetTarget(1, 6, .5)
else
springer:SetTarget(0, 2, .2)
end
switch = not switch
end
new
constructs a new Springer instance.
NOTE
The .new method is a constructor and should be called with a colon from the Springer ModuleScript.
Springer instances themselves, will not contain the .new method.