Looping Transitions

Okay so, I have been surfing the web for weeks to find the answer to this question:

How can I loop my transition for example, I make my object move to a random position but after it moved their I want it to go back to it’s starting position and repeat the transition. Please Help, Oh and I commented some things for me to go back to if I made a mistake

Here is my lengthyyyyy codeeee:

–[[— INITIAL VALUES -----]]

display.setStatusBar( display.HiddenStatusBar )

background = display.newImageRect( “background.png”, display.contentWidth, display.contentHeight+100 )

background.x = display.contentCenterX

background.y = display.contentCenterY

local physics = require “physics”

local btn = display.newImageRect(“button.png”,150,150)

local menuIcon = display.newImageRect( “menu.png”, 60, 60 )

menuIcon.x = display.contentCenterX-120

menuIcon.y = display.contentCenterY+240

local playsound = display.newImageRect( “playsound.png”, 50, 50)

playsound.x = display.contentCenterX + 90

playsound.y = display.contentCenterY - 260

local stopsound = display.newImageRect( “stopsound.png”, 50, 50)

stopsound.x = display.contentCenterX + 140

stopsound.y = display.contentCenterY - 260

– Trying to create an asteriod Shower

btn.x = math.random( 1,150 )

btn.y = -125

transition.to(btn, {

x = math.random( 150,300 ),

y = math.random( 700,800 ),

time = 5000,

})

–[[

for i=1,10 do

if btn.x >= 150 and btn.y >= 700 then

btn.x = math.random( 1,150 )

btn.y = -125

transition.to(btn, {

x = math.random( 150,300 ),

y = math.random( 700,800 ),

time = 5000,

})

end

end

]]

–[[

btn.x = display.contentWidth/2

btn.y = display.contentHeight/2

]]

amountOfTimeYouClicked = 0

testText = display.newText( amountOfTimeYouClicked, display.contentWidth/2, display.contentHeight/2 , “spaceage.ttf”, 30 )

testText:setFillColor( 250,223,0 )

test1Text = display.newText( "Clicks ", display.contentWidth/2, display.contentHeight/6 , “SPACEBAR.ttf”, 20 )

test1Text.x = display.contentCenterX/2.5

test1Text.y = display.contentCenterY-260

testText.x = display.contentCenterX-110

testText.y = display.contentCenterY-235

– audio

local sndTouch = audio.loadSound(“metal-clash.wav”)

local sndBackground = audio.loadStream( “backgroundmusic.ogg”)

local function audioloop()

if audio.isChannelActive(2) == true then

audio.resume()

else

audio.play(sndBackground, {channel = 2, loop = -1})

end

end

playsound:addEventListener( “touch”, audioloop)

local function stopaudioloop()

audio.pause()

end

stopsound:addEventListener( “touch”, stopaudioloop)

local function pushBtn(event)

if event.phase == “began” then

amountOfTimeYouClicked = amountOfTimeYouClicked + 1

print( amountOfTimeYouClicked )

testText.text = tostring(amountOfTimeYouClicked)

audio.play(sndTouch)

– Makes Asteriod Rotate

        transition.to( btn, { rotation=-45, time=500, transition=easing.inOutCubic } )

        transition.to( btn, { rotation=90, time=500, transition=easing.inOutCubic } )

        if btn.rotation == -45   then

        transition.to(btn, {rotation = 90, time = 500, transition=easing.inOutCubic})

        elseif btn.rotation == 90 then

          transition.to( btn, { rotation=-45, time=500, transition=easing.inOutCubic } )

        end

end

end

btn:addEventListener( “touch”, pushBtn )

local settings = display.newImageRect(“gear.png”, 65 , 65)

settings.x = display.contentCenterX + 120

settings.y = display.contentCenterY+240

local resetBtn = display.newImageRect(“reset.png”, 65, 65)

resetBtn.x = display.contentCenterX

resetBtn.y = display.contentCenterY+235

local function resetingBtn(event)

if event.phase == “began” then

print( 0 )

testText.text = 0

amountOfTimeYouClicked = testText.text

end

end

resetBtn:addEventListener( “touch”, resetingBtn)

– Trying to make a Asteriod Wrap around the Screen

–[[

– Makes Asteriod Rotate

btn.rotation = -45

local reverse = 1

local function rockRect()

    if ( reverse == 0 ) then

        reverse = 1

        transition.to( btn, { rotation=-45, time=500, transition=easing.inOutCubic } )

    else

        reverse = 0

        transition.to( btn, { rotation=90, time=500, transition=easing.inOutCubic } )

    end

end

  

timer.performWithDelay( 600, rockRect, 0 ) 

]]

Maybe this could help you…

transition.to( target, { transition=easing.continuousLoop, iterations = -1 } )

It’s a good temporary fix, BUT

  1. It goes to fast

  2. It isnt following the math.random so it doesnt look organic

BUT Thank you :slight_smile:

**UPDATED** 

Please format code (and log) posts:

formatyourcode.jpg

There are many, many, many ways to solve this.

This is just one:

local transitionTo transitionTo = function ( obj, fields2Mod, time, delay, easing ) -- Track current values of fields we wish to change local fields2Restore = {} for k,v in pairs(fields2Mod) do fields2Restore[k] = obj[k] end fields2Restore.time = time fields2Restore.delay = delay fields2Restore.transition = easing local params = {} for k,v in pairs(fields2Mod) do params[k] = v end params.time = time params.delay = delay params.transition = easing params.onComplete = obj function obj.onComplete( self ) self.onComplete = nil transition.to( obj, fields2Restore ) end transition.to( obj, params ) end local tmp = display.newCircle( 20, 20, 10 ) transitionTo( tmp, { x = 400, y = 500 }, 1000, 500, easing.outBounce )

The code above,

  1. Uses transition.to() to transition an object’s fields as requested.
  2. It stores the initial values of those fields.
  3. It uses onComplete, the stored fields, and transition.to() to reverse the original transition.

You need to modify this concept to meet your own needs.

Also, again, there are many other ways (some much more elegant) to achieve transitions that (as you call it) loop.

I say ‘as you call it’, because loop is a bit nebulous and may have slightly different meanings to you, I, and other readers.

Hopefully this gets you rolling on a solution.

More complicated (probably too complicated), but it loops and is random.

local transitionToLoop local function randomizeToFields() local fields2Mod = { x = math.random( 200, 500 ), y = math.random( 200, 500 ), alpha = math.random(), } local time = math.random( 500, 2000 ) local delay = math.random( 0, 500 ) local tmp = { easing.linear, easing.outBounce, easing.outBack } local myEasing = tmp[math.random(1,3)] return fields2Mod, time, delay, myEasing end transitionToLoop = function ( obj, fields2Mod, time, delay, myEasing ) -- Track current values of fields we wish to change local fields2Restore = {} for k,v in pairs(fields2Mod) do fields2Restore[k] = obj[k] end fields2Restore.time = time fields2Restore.delay = delay fields2Restore.transition = myEasing local params = {} for k,v in pairs(fields2Mod) do params[k] = v end params.time = time params.delay = delay params.transition = myEasing params.onComplete = obj function obj.onComplete( self ) self.onComplete = function() local fields2Mod, time, delay, myEasing = randomizeToFields() transitionToLoop( self, fields2Mod, time, delay, myEasing ) end fields2Restore.onComplete = self transition.to( obj, fields2Restore ) end transition.to( obj, params ) end local tmp = display.newCircle( 20, 20, 10 ) local fields2Mod, time, delay, myEasing = randomizeToFields() transitionToLoop( tmp, fields2Mod, time, delay, myEasing )

The limits here are experience, creativity, and willingness to experiment.  

You can do this.

Final note (I do this a lot it seems).

If you’re simply trying to solve the puzzle of screen wrapping (like in asteroids), using transitions is not the way to go.

Use physics, collisions, and math instead.  You’re already going to be using physics for collisions, so don’t combine that with transitions (because that is a bad mix; causes unexpected results)

Or use SSK 2 when you get up to speed on Corona.

actions.gif

SSK 2 comes with a scene wrapping helper(and you can look at the code) that makes implementing screen wrap this easy:

-- Create a rectangle to act as our 'wrapping bounds' local wrapProxy = display.newRect( display.contentCenterX, display.contentCenterY, 300, 300 ) -- Show it a little so we can see where the wrap should happen wrapProxy.alpha = 0.1 -- Place a player in the center of the wrapProxy and give it a random velocity local player = display.newCirle( wrapProxy.x, wrapProxy.y, 20 ) physics:addBody( player ) player:setLinearVelocity( math.random( -100, 100), math.random( -100, 100) ) -- Start an enter frame listener and test for wrapping each frame function player.enterFrame( self ) ssk.actions.scene.rectWrap( self, wrapProxy ) end Runtime:addEventListener( "enterFrame", player )

Remember, SSK2 is really only going to be useful to you once you already know how to use Corona.

Please take some time to understand the fundamentals:

  • Lua
  • These core libraries/modules
    • display.* 
    • transition.*
    • timer.*
    • physics.*
  • Events & Listeners

Know where the API docs root is: https://docs.coronalabs.com/api/

Also be sure you have at least perused the topics in tutorials and guides.

Once you are familiar with all these things, then consider using all or part of SSK 2.

Maybe this could help you…

transition.to( target, { transition=easing.continuousLoop, iterations = -1 } )

It’s a good temporary fix, BUT

  1. It goes to fast

  2. It isnt following the math.random so it doesnt look organic

BUT Thank you :slight_smile:

**UPDATED** 

Please format code (and log) posts:

formatyourcode.jpg

There are many, many, many ways to solve this.

This is just one:

local transitionTo transitionTo = function ( obj, fields2Mod, time, delay, easing ) -- Track current values of fields we wish to change local fields2Restore = {} for k,v in pairs(fields2Mod) do fields2Restore[k] = obj[k] end fields2Restore.time = time fields2Restore.delay = delay fields2Restore.transition = easing local params = {} for k,v in pairs(fields2Mod) do params[k] = v end params.time = time params.delay = delay params.transition = easing params.onComplete = obj function obj.onComplete( self ) self.onComplete = nil transition.to( obj, fields2Restore ) end transition.to( obj, params ) end local tmp = display.newCircle( 20, 20, 10 ) transitionTo( tmp, { x = 400, y = 500 }, 1000, 500, easing.outBounce )

The code above,

  1. Uses transition.to() to transition an object’s fields as requested.
  2. It stores the initial values of those fields.
  3. It uses onComplete, the stored fields, and transition.to() to reverse the original transition.

You need to modify this concept to meet your own needs.

Also, again, there are many other ways (some much more elegant) to achieve transitions that (as you call it) loop.

I say ‘as you call it’, because loop is a bit nebulous and may have slightly different meanings to you, I, and other readers.

Hopefully this gets you rolling on a solution.

More complicated (probably too complicated), but it loops and is random.

local transitionToLoop local function randomizeToFields() local fields2Mod = { x = math.random( 200, 500 ), y = math.random( 200, 500 ), alpha = math.random(), } local time = math.random( 500, 2000 ) local delay = math.random( 0, 500 ) local tmp = { easing.linear, easing.outBounce, easing.outBack } local myEasing = tmp[math.random(1,3)] return fields2Mod, time, delay, myEasing end transitionToLoop = function ( obj, fields2Mod, time, delay, myEasing ) -- Track current values of fields we wish to change local fields2Restore = {} for k,v in pairs(fields2Mod) do fields2Restore[k] = obj[k] end fields2Restore.time = time fields2Restore.delay = delay fields2Restore.transition = myEasing local params = {} for k,v in pairs(fields2Mod) do params[k] = v end params.time = time params.delay = delay params.transition = myEasing params.onComplete = obj function obj.onComplete( self ) self.onComplete = function() local fields2Mod, time, delay, myEasing = randomizeToFields() transitionToLoop( self, fields2Mod, time, delay, myEasing ) end fields2Restore.onComplete = self transition.to( obj, fields2Restore ) end transition.to( obj, params ) end local tmp = display.newCircle( 20, 20, 10 ) local fields2Mod, time, delay, myEasing = randomizeToFields() transitionToLoop( tmp, fields2Mod, time, delay, myEasing )

The limits here are experience, creativity, and willingness to experiment.  

You can do this.

Final note (I do this a lot it seems).

If you’re simply trying to solve the puzzle of screen wrapping (like in asteroids), using transitions is not the way to go.

Use physics, collisions, and math instead.  You’re already going to be using physics for collisions, so don’t combine that with transitions (because that is a bad mix; causes unexpected results)

Or use SSK 2 when you get up to speed on Corona.

actions.gif

SSK 2 comes with a scene wrapping helper(and you can look at the code) that makes implementing screen wrap this easy:

-- Create a rectangle to act as our 'wrapping bounds' local wrapProxy = display.newRect( display.contentCenterX, display.contentCenterY, 300, 300 ) -- Show it a little so we can see where the wrap should happen wrapProxy.alpha = 0.1 -- Place a player in the center of the wrapProxy and give it a random velocity local player = display.newCirle( wrapProxy.x, wrapProxy.y, 20 ) physics:addBody( player ) player:setLinearVelocity( math.random( -100, 100), math.random( -100, 100) ) -- Start an enter frame listener and test for wrapping each frame function player.enterFrame( self ) ssk.actions.scene.rectWrap( self, wrapProxy ) end Runtime:addEventListener( "enterFrame", player )

Remember, SSK2 is really only going to be useful to you once you already know how to use Corona.

Please take some time to understand the fundamentals:

  • Lua
  • These core libraries/modules
    • display.* 
    • transition.*
    • timer.*
    • physics.*
  • Events & Listeners

Know where the API docs root is: https://docs.coronalabs.com/api/

Also be sure you have at least perused the topics in tutorials and guides.

Once you are familiar with all these things, then consider using all or part of SSK 2.