Skip to content

Commit d859260

Browse files
committed
Tween: Fix wrong function order
1 parent f0f3c7e commit d859260

File tree

1 file changed

+56
-61
lines changed

1 file changed

+56
-61
lines changed

custom_events/Tween.lua

Lines changed: 56 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@
4848
-- #####################################################################
4949
-- [[ Tween Configuration Tables ]]
5050
-- #####################################################################
51-
5251
local validTweenTypes = {"Alpha", "Angle", "X", "Y", "Color", "Zoom", "Scale"}
5352
local tweenAltNames = {
5453
Rotate = "Angle",
@@ -69,67 +68,9 @@ end
6968
-- #####################################################################
7069
-- [[ Custom Tween Implementation: Scale ]]
7170
-- #####################################################################
72-
7371
local activeTweens = {}
7472
local tweenUpdateRate = 1 / 60 -- 60 FPS update rate
7573

76-
-- Custom implementation of scale tweening using runTimer
77-
local function doTweenScale(tag, object, toValue, duration, easeType)
78-
-- Get current scale values
79-
local startScaleX = getProperty(object .. ".scale.x")
80-
local startScaleY = getProperty(object .. ".scale.y")
81-
82-
if startScaleX == nil or startScaleY == nil then
83-
debugPrint('Tween Error: Object "' .. object .. '" does not exist or has no scale property')
84-
return false
85-
end
86-
87-
-- Store tween data
88-
activeTweens[tag] = {
89-
object = object,
90-
startScaleX = startScaleX,
91-
startScaleY = startScaleY,
92-
targetScale = toValue,
93-
duration = duration,
94-
easeType = easeType,
95-
elapsed = 0,
96-
isActive = true
97-
}
98-
99-
-- Start the update timer
100-
runTimer("tween_scale_" .. tag, tweenUpdateRate, 0)
101-
return true
102-
end
103-
104-
-- Update function for scale tweens
105-
local function updateScaleTween(tag, elapsed)
106-
local tween = activeTweens[tag]
107-
if not tween or not tween.isActive then
108-
return false
109-
end
110-
111-
tween.elapsed = tween.elapsed + elapsed
112-
local progress = math.min(tween.elapsed / tween.duration, 1)
113-
local easedProgress = applyEasing(progress, tween.easeType)
114-
local currentScale = tween.startScaleX + (tween.targetScale - tween.startScaleX) * easedProgress
115-
116-
setProperty(tween.object .. ".scale.x", currentScale)
117-
setProperty(tween.object .. ".scale.y", currentScale)
118-
119-
if progress >= 1 then
120-
tween.isActive = false
121-
cancelTimer("tween_scale_" .. tag)
122-
123-
if onTweenCompleted then
124-
onTweenCompleted(tag)
125-
end
126-
127-
return false
128-
end
129-
130-
return true
131-
end
132-
13374
-- Custom implementation of easing functions for the custom scale tween
13475
-- Mimics easing functions found in FlxEase
13576
local easingFunctions = {}
@@ -281,11 +222,66 @@ local function applyEasing(t, easeType)
281222
return easingFunc and easingFunc(t) or t
282223
end
283224

225+
-- Custom implementation of scale tweening using runTimer
226+
local function doTweenScale(tag, object, toValue, duration, easeType)
227+
-- Get current scale values
228+
local startScaleX = getProperty(object .. ".scale.x")
229+
local startScaleY = getProperty(object .. ".scale.y")
230+
231+
if startScaleX == nil or startScaleY == nil then
232+
debugPrint('Tween Error: Object "' .. object .. '" does not exist or has no scale property')
233+
return false
234+
end
235+
236+
-- Store tween data
237+
activeTweens[tag] = {
238+
object = object,
239+
startScaleX = startScaleX,
240+
startScaleY = startScaleY,
241+
targetScale = toValue,
242+
duration = duration,
243+
easeType = easeType,
244+
elapsed = 0,
245+
isActive = true
246+
}
247+
248+
-- Start the update timer
249+
runTimer("tween_scale_" .. tag, tweenUpdateRate, 0)
250+
return true
251+
end
252+
253+
-- Update function for scale tweens
254+
local function updateScaleTween(tag, elapsed)
255+
local tween = activeTweens[tag]
256+
if not tween or not tween.isActive then
257+
return false
258+
end
259+
260+
tween.elapsed = tween.elapsed + elapsed
261+
local progress = math.min(tween.elapsed / tween.duration, 1)
262+
local easedProgress = applyEasing(progress, tween.easeType)
263+
local currentScale = tween.startScaleX + (tween.targetScale - tween.startScaleX) * easedProgress
264+
265+
setProperty(tween.object .. ".scale.x", currentScale)
266+
setProperty(tween.object .. ".scale.y", currentScale)
267+
268+
if progress >= 1 then
269+
tween.isActive = false
270+
cancelTimer("tween_scale_" .. tag)
271+
272+
if onTweenCompleted then
273+
onTweenCompleted(tag)
274+
end
275+
276+
return false
277+
end
278+
279+
return true
280+
end
284281

285282
-- #####################################################################
286283
-- [[ Event Functions ]]
287284
-- #####################################################################
288-
289285
-- Parses the tween type, tag, and object from the input string.
290286
local function parseTweenNames(value)
291287
local cleanValue = value:gsub(" ", "")
@@ -355,7 +351,6 @@ end
355351
-- #####################################################################
356352
-- [[ Bind our local functions to Psych Engine events ]]
357353
-- #####################################################################
358-
359354
function onEvent(name, value1, value2)
360355
if name == "Tween" then
361356
local tweenType, tag, object = parseTweenNames(value1)

0 commit comments

Comments
 (0)