infinite loop

6 replies [Last post]
oamer_11
User offline. Last seen 7 weeks 15 hours ago. Offline
Joined: 7 Oct 2011

hello there

i want to move an object for infinite loop .. just like a sky to go from point to another point

is that right ?

"
while true do
transition.to( sky, { time=3000, x=80, y=55 } )
local function listener( event )
transition.to( sky, { time=3000, x=385, y=55 } )

end
timer.performWithDelay(3000, listener )
end
"

Replies

horacebury
User offline. Last seen 20 min ago. Offline
Joined: 17 Aug 2010

Actually, you're better off using the onComplete parameter of the transition.to function:

1
2
3
4
local function repeatLoop()
        transition.to( obj, { time=5000, onComplete=repeatLoop } )
end
repeatLoop()

oamer_11
User offline. Last seen 7 weeks 15 hours ago. Offline
Joined: 7 Oct 2011

i dont know to use the repeatLoop ..

so can u guide me how to use it to as example move a object from x= 250 to x=50 then get back to the initial point

Anderoth
User offline. Last seen 14 hours 25 min ago. Offline
Joined: 1 May 2011

Something like this would do what you are describing.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
-- load sky graphic
local sky = display.newImage("sky.png", true)
sky.x = 250
 
-- function to reset and move sky object
local function moveSky()
        -- reset to starting location
        sky.x = 250
 
        -- transition sky, when finished start over again
        transition.to(sky,{time=5000, x=50, onComplete=moveSky})
end
 
moveSky() -- start moving sky

horacebury
User offline. Last seen 20 min ago. Offline
Joined: 17 Aug 2010

In my code 'repeatLoop' is not anything special - its just the name I chose for the function.

What's happening in my code (and in the next sample posted) is a regular transition (Corona library function) is being used to move the sky image by gradually altering the .x value. Then, when the transition finishes, it will automatically call the function named in the 'onComplete' parameter. Because the transition is started in that same function, it starts again, like an infinite loop.

The short of it is: When the animation (transition) finishes, it calls itself to start again. The magic bit is the 'onComplete' parameter.

oamer_11
User offline. Last seen 7 weeks 15 hours ago. Offline
Joined: 7 Oct 2011

ya thats right >>> but i want

sky.x = 250.. to be transition way to get back to x =250

how can i do this ?

Brent Sorrentino
User offline. Last seen 10 hours 28 min ago. Offline
Staff
Joined: 28 Nov 2012

Hello @oamer_11,
Note that you can "stack" transitions as well, putting a delay on the next in the stack so it executes after the first one is done.

1
2
3
sky.x = 250
transition.to(sky,{time=5000, x=50 })
transition.to(sky,{time=5000, x=250, delay=5000})  --delay of 5000 to equal time of 1st transition

The caveat is that this approach can get messy if you have dozens of transitions going on, and/or if you need to pause them or cancel them before they complete. If that is your intention, then you are better off making a more robust solution using the "onComplete" methods and a tracking variable to determine which transition is next in the queue.

Brent

Viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.