How to structure code with long transition sequences ?

Dears,

I have long transition sequences where next transition starts right after previous finish. I use onComplete=… on transitions but code becomes unreadble because of jumps to next functions and there is how to say no clear line of execution … May be u know how to manage it … some link … or may be Rob Miracle knows :slight_smile:

please, help :slight_smile:

Best Regards, 

Oleg

Not sure what you mean exactly but I use anonymous functions in most cases, something like this:

transition.to(object, {time=2000, y=someValue, onComplete=function(o) transition.to(o, {time=500, alpha=0, onComplete=function(o) --do something else }) })

Thank u, but if there are a lot of code inline function doesn’t help. Moreover if there another inline function inside and so on code will be awful … But thank u again for your participation.  :slight_smile:

There are ways to chain your transitions. I made this code for another thread recently, maybe it helps you along. You can copy paste it into a main.lua and run it to see how it works.

[lua]

local player = display.newRect(111,111,30,30)

local bg = display.newRect(111,300,500,800)

bg:setFillColor(0.2,0.4,0.2)

local foods = {}

local goingToFood = 0

local currentlyTransition = false

local goToNext

local function goToFood()

    if foods[goingToFood+1] and not currentlyTransition then

        goingToFood = goingToFood + 1

        currentlyTransition = true

        transition.to(player,{onComplete = goToNext, tag = “playerTransition”, time = 2000, x = foods[goingToFood].x, y = foods[goingToFood].y})

    end

end

function goToNext()

    currentlyTransition = false

    goToFood()

end

local function touch(e)

    if e.phase == “began” then

        foods[#foods+1] = display.newCircle(0,0,5)

        foods[#foods].x , foods[#foods].y = e.x , e.y

        goToFood()

    end

end

bg:toBack()

bg:addEventListener(“touch”,touch)

[/lua]

Simplified version:

[lua]local player = display.newRect(111,111,30,30)

local destinations = {

    {x = 200, y = 200},

    {x = 100, y = 100},

    {x = 200, y = 300}

}

local currentlyGoingTo = 0

local function startTransition()

    currentlyGoingTo = currentlyGoingTo + 1

    if destinations[currentlyGoingTo] then

        transition.to(player, {x = destinations[currentlyGoingTo].x, y = destinations[currentlyGoingTo].y, onComplete = startTransition})

    end

end

startTransition()[/lua]

thank you !

Encapsulated version - same idea as Jon’s basically - simple enough but easy to expand to allow named movements, repeats etc. Each object has a list of transitions associated with it - if no onComplete is defined in the transition one is added to call the get-the-next-transition function.

Simple demo showing a couple of circles being animated.

(any chance of the forum being fixed so it doesn’t automatically remove indents ???)

-- -- List of current transitions - each has a list (list of transitions) -- and an index (current transition) -- -- transitions can have onComplete defined, if not will go automatically -- to next. local transitionLists = {}&nbsp; function defineExtTransition(displayObject, transitionList) transitionLists[displayObject] = {} transitionLists[displayObject].list = transitionList transitionLists[displayObject].index = 0 end function runExtTransition(displayObject) assert(transitionLists[displayObject] ~= nil) local n = transitionLists[displayObject].index + 1 if n \<= #transitionLists[displayObject].list then transitionLists[displayObject].index = n&nbsp; local tdef = transitionLists[displayObject].list[n] tdef.onComplete = tdef.onComplete or runExtTransition transition.to(displayObject,tdef) else&nbsp; transitionLists[displayObject] = nil&nbsp; end end -- DEMO USAGE -- create a couple of circles local ob1 = display.newCircle(160,240,15) ob1:setFillColor( 1,1,0 ) local ob2 = display.newCircle(10,10,15) ob2:setFillColor( 0,1,0 ) -- what ob1 will do - wander about aimlessly, stops by itself defineExtTransition(ob1, {&nbsp; { time = 1000, x = 0, y = 0}, { time = 500, x = 0, y = 400 }, { time = 2500, x = 320, y = 0 } }) -- what ob2 will do - round edge of screen, prints hello world when done defineExtTransition(ob2, { { time = 1000, x = 310, y = 10}, { time = 1000, x = 310 , y = 470}, { time = 1000, x = 0, y = 470 }, { time = 1000, x = 10, y = 10, onComplete = function() print("Hello, world!") end&nbsp;} }) -- now go do it ! runExtTransition(ob1) runExtTransition(ob2)

This tutorial might be of some use:

http://coronalabs.com/blog/2014/01/07/tutorial-moving-objects-along-a-path/

It shows how to queue up multiple transitions.

Thank you, very much. Nice to feel that a lot of people are ready to help !!! 

Not sure what you mean exactly but I use anonymous functions in most cases, something like this:

transition.to(object, {time=2000, y=someValue, onComplete=function(o) transition.to(o, {time=500, alpha=0, onComplete=function(o) --do something else }) })

Thank u, but if there are a lot of code inline function doesn’t help. Moreover if there another inline function inside and so on code will be awful … But thank u again for your participation.  :slight_smile:

There are ways to chain your transitions. I made this code for another thread recently, maybe it helps you along. You can copy paste it into a main.lua and run it to see how it works.

[lua]

local player = display.newRect(111,111,30,30)

local bg = display.newRect(111,300,500,800)

bg:setFillColor(0.2,0.4,0.2)

local foods = {}

local goingToFood = 0

local currentlyTransition = false

local goToNext

local function goToFood()

    if foods[goingToFood+1] and not currentlyTransition then

        goingToFood = goingToFood + 1

        currentlyTransition = true

        transition.to(player,{onComplete = goToNext, tag = “playerTransition”, time = 2000, x = foods[goingToFood].x, y = foods[goingToFood].y})

    end

end

function goToNext()

    currentlyTransition = false

    goToFood()

end

local function touch(e)

    if e.phase == “began” then

        foods[#foods+1] = display.newCircle(0,0,5)

        foods[#foods].x , foods[#foods].y = e.x , e.y

        goToFood()

    end

end

bg:toBack()

bg:addEventListener(“touch”,touch)

[/lua]

Simplified version:

[lua]local player = display.newRect(111,111,30,30)

local destinations = {

    {x = 200, y = 200},

    {x = 100, y = 100},

    {x = 200, y = 300}

}

local currentlyGoingTo = 0

local function startTransition()

    currentlyGoingTo = currentlyGoingTo + 1

    if destinations[currentlyGoingTo] then

        transition.to(player, {x = destinations[currentlyGoingTo].x, y = destinations[currentlyGoingTo].y, onComplete = startTransition})

    end

end

startTransition()[/lua]

thank you !

Encapsulated version - same idea as Jon’s basically - simple enough but easy to expand to allow named movements, repeats etc. Each object has a list of transitions associated with it - if no onComplete is defined in the transition one is added to call the get-the-next-transition function.

Simple demo showing a couple of circles being animated.

(any chance of the forum being fixed so it doesn’t automatically remove indents ???)

-- -- List of current transitions - each has a list (list of transitions) -- and an index (current transition) -- -- transitions can have onComplete defined, if not will go automatically -- to next. local transitionLists = {}&nbsp; function defineExtTransition(displayObject, transitionList) transitionLists[displayObject] = {} transitionLists[displayObject].list = transitionList transitionLists[displayObject].index = 0 end function runExtTransition(displayObject) assert(transitionLists[displayObject] ~= nil) local n = transitionLists[displayObject].index + 1 if n \<= #transitionLists[displayObject].list then transitionLists[displayObject].index = n&nbsp; local tdef = transitionLists[displayObject].list[n] tdef.onComplete = tdef.onComplete or runExtTransition transition.to(displayObject,tdef) else&nbsp; transitionLists[displayObject] = nil&nbsp; end end -- DEMO USAGE -- create a couple of circles local ob1 = display.newCircle(160,240,15) ob1:setFillColor( 1,1,0 ) local ob2 = display.newCircle(10,10,15) ob2:setFillColor( 0,1,0 ) -- what ob1 will do - wander about aimlessly, stops by itself defineExtTransition(ob1, {&nbsp; { time = 1000, x = 0, y = 0}, { time = 500, x = 0, y = 400 }, { time = 2500, x = 320, y = 0 } }) -- what ob2 will do - round edge of screen, prints hello world when done defineExtTransition(ob2, { { time = 1000, x = 310, y = 10}, { time = 1000, x = 310 , y = 470}, { time = 1000, x = 0, y = 470 }, { time = 1000, x = 10, y = 10, onComplete = function() print("Hello, world!") end&nbsp;} }) -- now go do it ! runExtTransition(ob1) runExtTransition(ob2)

This tutorial might be of some use:

http://coronalabs.com/blog/2014/01/07/tutorial-moving-objects-along-a-path/

It shows how to queue up multiple transitions.

Thank you, very much. Nice to feel that a lot of people are ready to help !!!