Share Your Code

Pausable Transitions - A very simple method

Posted by Satheesh, Posted on November 26, 2011, Last updated November 28, 2011

GitHub URL: 
https://github.com/SatheeshJM/simple_transition_pausing_from_Satheesh

Yeah I know there are a lot of transition managers.. Still never hurts to share code.. So here goes.

USAGE :

1
2
3
transition.to(object,params}
transition.pause(object)    
transition.resume(object)

I am not sure about the efficiency of this module. So if anyone has any modifications or if anyone finds any bug, please comment below.

And I haven't tested this completely, but till now, it has worked fine for me :)

https://github.com/SatheeshJM/simple_transition_pausing_from_Satheesh

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
-- A very Simple Pausable Transition
-- Author: Satheesh
-- Release date: 2011-11-27
-- Version: 1.0
-- License: MIT
-- Web: http:www.timeplusq.com
--
-- USAGE:
--      Import the module :
--              require "simple_transition_pausing_from_Satheesh"
--
--      Create transitions as usual
--              transition.to(object,params}
--
--              transition.pause(object)     pauses all transitions of object
--              transition.resume(object)    resumes all transitions of object
--
--
--
-- PROS
--      No extra code. Just use transitions as you used before
--
-- CONS 
--      Delta transitions,easing not supported
--           (to be frank, i have no idea what they do :P)
--      May have minor bugs, since i haven't tested completely.
--           (works fine for my requirements though.. if you find any bug please let me know)
 
 
 
 
 
--The transition.to and transiion.cancel functions are duplicated
local transitionToCopy = transition.to
local transitionCancelCopy = transition.cancel
 
--I override transition.to function with my function myTransitionFunction
--the duplicate i create is used to call the transition
local myTransitionFunction = function(object,params)
        if not object.destination then
                object.destination = {}
        end
        if not object.startTime then
                object.startTime = {}
        end
        if not object.transition then
                object.transition = {}
        end
                
 
        --object.destination - table storing transition parameters of each transition of the object
        
        local transitionNo = #object.destination+1
        object.destination      [transitionNo] = {}
        
        local destination = object.destination[transitionNo]
                        
        destination.x = params.x
        destination.y = params.y
        destination.xScale = params.xScale
        destination.yScale = params.yScale
        destination.time = params.time
        destination.delay = params.delay
        destination.onComplete = params.onComplete
        destination.onStart = params.onStart
        destination.alpha = params.alpha
        destination.width = params.width
        destination.height = params.height
        destination.rotation = params.rotation
        destination.xOrigin = params.xOrigin
        destination.yOrigin = params.yOrigin
        destination.maskX = params.maskX
        destination.maskY = params.maskY
        destination.maskScaleX = params.maskScaleX
        destination.maskScaleY = params.maskScaleY
        destination.maskRotation = params.maskRotation
        destination.xReference = params.xReference
        destination.yReference = params.yReference
        
        if destination.delay== nil then
                destination.delay=0
        end
        
        
        --on transition complete, deletes the corresponding table values for that particular transition 
        --and calls the onComplete function which was specified by the user
        
        local onComplete = params.onComplete
        params.onComplete = function(obj)       
                local localDestination = object.destination
                for i =1,#localDestination do   
                        if localDestination[i]==destination then
                                table.remove(object.destination,i)
                                table.remove(object.startTime,i)
                                table.remove(object.transition,i)
                                break
                        end
                end             
                if onComplete then
                        onComplete(obj)
                end
        end
        
        
        --object.startTime  - table storing starting times of each transition of the object
        --object.transition - table storing transition handles of each transition of the object
        
        object.transition[transitionNo] = transitionToCopy(object,params)
        object.transition[transitionNo].object = object
        object.startTime[transitionNo] = system.getTimer()
        
        return object.transition[transitionNo] 
 
end
 
 
--Function that pauses all transitions of an object
 
local pause = function(object)
 
        --object.pausedDestination - table storing transition parameters of each transition with time and delay adjusted after pause
        object.pauedDestination = {}
        
        --for each transition
        for i =1,#object.startTime do
                local object = object
                local destination = object.destination[i]
                local objTransition = object.transition[i]
                local startTime = object.startTime[i]
                
                object.pauedDestination[i] = destination
                local pauedDestination = object.pauedDestination[i]
                
                                
                local currentTime = system.getTimer()
                local delay = destination.delay
                
                --time and delay adjustment
                if (startTime+delay) > currentTime then
                        pauedDestination.delay = (startTime+delay)-currentTime                  
                else
                        local timeRemaining = (startTime+delay+destination.time) - currentTime
                        pauedDestination.delay = nil
                        pauedDestination.time = timeRemaining
                end
                
                --cancel transition             
                transitionCancelCopy(objTransition)
        end
        
        --remove all transition properties of the object from the respective tables
        object.destination = nil
        object.objTransition = nil
        object.startTime = nil
end
        
--resumes all paused transitions of the object
local resume = function(object)
 
        --pausedDestination holds all transition parameters with adjustments for time and delay
        local pauedDestination = object.pauedDestination
        for i =1,#pauedDestination do
                myTransitionFunction(object,pauedDestination[i])
        end
end
 
--I override transition.cancel function with my function cancel
--the duplicate i created is used to cencel the transition
local cancel = function(transition)
        local object = transition.object
        local localTransition= object.transition
        for i =1,#localTransition do    
                if localTransition[i]==transition then
                        table.remove(object.destination,i)
                        table.remove(object.startTime,i)
                        table.remove(object.transition,i)
                        break
                end
        end     
        transitionCancelCopy(transition)
end
 
--overriding transition.to and transition.cancel
--assigning transition.pause and transition.resume
transition.to=myTransitionFunction
transition.pause = pause
transition.resume = resume
transition.cancel = cancel


Replies

rahulsingh2k10
User offline. Last seen 36 weeks 17 hours ago. Offline
Joined: 9 Aug 2011

can I pause transition.dissolve using the above method.If so ,how can I do it.I have two transition.dissolve

al=transition.dissolve(blackText[i],redText[i],trans1,delay1)
bl=transition.dissolve(redText[i],blackText[i],trans2,delay2)

Now I need to pause and resume the above transitions.How can i do it?
Coz when I did what u have said in the following method I am getting an error

Copyright (C) 2009-2011 A n s c a , I n c .
Version: 2.0.0
Build: 2011.591
--------------------------------------------------------------------
Runtime error
...\simple_transition_pausing_from_Satheesh.lua:122: attempt to
index local 'object' (a nil value)
stack traceback:
[C]: ?
...\simple_transition_pausing_from_Satheesh.lua:122: in function
'pause'
...sers\Rahul Singh\Deskt

Please take me out of this problem

Satheesh
User is online Online
Joined: 25 May 2011

I haven't included pausing of transition.dissolve in the module.. will try to include that too!

That and pause-resuming of individual transitions coming up!

rahulsingh2k10
User offline. Last seen 36 weeks 17 hours ago. Offline
Joined: 9 Aug 2011

ok..Looking forward to it...:)

PerturbedMollusk
User offline. Last seen 1 year 22 weeks ago. Offline
Joined: 24 Nov 2011

I am moving a display group

1
2
3
4
5
6
7
8
function moveGroup(params)
        moveOrderTime = params.time
        moveOrderX = params.x
        moveOrderY = params.y
        output = transition.to(displayGroup,{time = moveOrderTime, x = moveOrderX, y = moveOrderY, onComplete = resetGroup})
                
                return output
end

then trying to pause it when it collides with something, and resume when the collision has ended

1
2
3
4
5
6
7
8
function onLocalCollision(event)
        if (event.phase=="began") then
                transition.pause(displayGroup)
        end
        if (event.phase=="ended") then
                transition.resume(displayGroup)
        end
end

but it complains that pause is a nil value.

EDIT: solved it, the module hadn't saved -_-

rahulsingh2k10
User offline. Last seen 36 weeks 17 hours ago. Offline
Joined: 9 Aug 2011

is there a way to pause and resume a table of elements using this ?eg.

1
transition.to(blackText[i],{delay=delay1,time=trans1,alpha=0})

and then you can pause and resume this using
transition.pause(blackText[i]) and transition.resume(blackText[i])

Is this possible?

kreso
User offline. Last seen 2 weeks 9 hours ago. Offline
Joined: 20 Dec 2009

I made a very simple script to pause/resume transition without using any fancy scripting. It works with custom as well as built in easing functions and is easily adjustable.

The way transition works is it saves a table with a bunch of useful information like 'start keys' (position, rotation, etc..) and 'finish keys' (where position, rotation should end). Transition object also saves 'start time' when animation should start and of course - the object transitioning. Having these it is easy to manipulate transitions the way we want.

Script works like this:
1. Copy all keys (rotation, position...) to a temp variable
2. set current position/rotation ... as 'start keys'
3. set animation start time in distant future (steps 2&3 will make animation seem paused)

To unpause simply copy back the old key values and calculate animation start time.

Here is the script:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
GameTransition = {}
 
function GameTransition:new(obj, params)
    
    
    local gameTransition = transition.to(obj, params)
    
    function gameTransition:cancelMe()
        transition.cancel(self)
    end
    
    return gameTransition
    
end
 
 
function GameTransition:pauseAll()
    local stopTime = system.getTimer()
    
    for i,v in pairs(Game.gameTransitions) do
        v.timeElapsed =  stopTime - v._timeStart
        v._timeStart = stopTime + 172800000 --animation will start in roughly 2 days, until then - wait
        --save a copy of originating keys
        v.savedKeysStart = copyTableSimple(v._keysStart)
        --copy current keys (location, rotation...) in place of start keys
        for j,z in pairs(v._keysStart) do
            v._keysStart[j] = v._target[j]
        end
    end
end
 
 
function GameTransition:resumeAll()
   
    local timeNow = system.getTimer()
    
    for i,v in pairs(Game.gameTransitions) do
       --calculate how much in past is 'start time' now
        v._timeStart = timeNow - v.timeElapsed
        --copy saved start keys back
        for j,z in pairs(v.savedKeysStart) do
            v._keysStart[j] = z
        end
    end
end
 
 
 
return GameTransition

Edit: just remembered I am using my custom function to copy table:

1
2
3
4
5
6
7
8
9
10
11
function copyTableSimple(t)
    local c = { }
    for k, v in pairs(t) do
        if type(v) == "table" then
            c[k] = copyTableSimple(v)
        else
            c[k] = v
        end
    end
    return c
end