Loop or Event

Hi all, 

I’m can’t find the code on how to make and object, say a board move back and forth. It would be 1 board going back and forth, I’m a bit confused.

Appreciate any help, 

Thanks

I would use a transition which repeats. Take a look at the transition.to documentation to work this out. You might need two transitions, for moving in each direction, with maybe a pause. That can be achieved with onComplete events on the transition call, etc.

Something like this would probably do it (untested):

-- Create the board and position it at the left hand side of the screen local board = display.newRect(0, display.contentCenterY, 100, 20) board.anchorX = 0 -- Declare functions as variables to be able to cross-reference from onComplete functions local goLeft, goRight goLeft = function() transition.to(board, { x = 0, time = 1000, onComplete = function() goRight() end }) end goRight = function() transition.to(board, { x = display.contentWidth - board.width, time = 1000, onComplete = function() goLeft() end }) end -- Start moving the board to the right goRight()

As a side note, I wrote a lib called transition2 which makes these kind of transitions easier which you can find here: https://github.com/rannerboy/corona-transition2. I’d suggest that you start with the example above using the original transition library, but if you feel like trying out transition2 you could replace the goLeft/goRight functions with this instead:

transition2.to(board, { x = display.contentWidth - board.width, time = 1000, reverse = true, -- This will reverse the transition when the board reaches it's final position, i.e. moving the board back to x=0 iterations = 0, -- Repeat forever })

If I may, here’s a slight refactoring:

-- Create the board and position it at the left hand side of the screen local board = display.newRect(0, display.contentCenterY, 100, 20) board.anchorX = 0 -- Declare functions as variables to be able to cross-reference from onComplete functions local goLeft, goRight goLeft = function() transition.to(board, { x = 0, time = 1000, onComplete = goRight }) end goRight = function() transition.to(board, { x = display.contentWidth - board.width, time = 1000, onComplete = goLeft }) end -- Start moving the board to the right goRight()

This works because the forward reference to goLeft and goRight allows the functions to address the as-yet-undefined functions when the code actually runs. The onComplete property of the transition.to parameters object is a function reference.

We did a series of tutorials on this topic in the path. There were three of them:

  1. Moving using transition.to() (tutorial is not available today)

  2. Moving using an enterFrame listener: https://coronalabs.com/blog/2015/08/25/tutorial-animation-with-enterframe-listeners/

  3. Moving using physics (tutorial was unpublished)

I’m not sure why we are missing the two tutorials, but…   The gist of the 3rd option was to use two “.isSensor” invisible objects that define the range of movement you want your object to have and use collision events to determine when you need to change direction. When the collision is detected, you do a setLinearVelocity on the object in the direction you want to go.

All three methods have their pros and cons, and you will need to figure out what works best for you.

Rob

You could also combine the options of display object transitions with physics objects being moved and use a touch joint to control the physics object’s position but setting it’s target location to that of a transitioned display object. (Ask me and I’ll break that explanation down.)

Thanks everybody for helping me out, thanks horacebury.

I would use a transition which repeats. Take a look at the transition.to documentation to work this out. You might need two transitions, for moving in each direction, with maybe a pause. That can be achieved with onComplete events on the transition call, etc.

Something like this would probably do it (untested):

-- Create the board and position it at the left hand side of the screen local board = display.newRect(0, display.contentCenterY, 100, 20) board.anchorX = 0 -- Declare functions as variables to be able to cross-reference from onComplete functions local goLeft, goRight goLeft = function() transition.to(board, { x = 0, time = 1000, onComplete = function() goRight() end }) end goRight = function() transition.to(board, { x = display.contentWidth - board.width, time = 1000, onComplete = function() goLeft() end }) end -- Start moving the board to the right goRight()

As a side note, I wrote a lib called transition2 which makes these kind of transitions easier which you can find here: https://github.com/rannerboy/corona-transition2. I’d suggest that you start with the example above using the original transition library, but if you feel like trying out transition2 you could replace the goLeft/goRight functions with this instead:

transition2.to(board, { x = display.contentWidth - board.width, time = 1000, reverse = true, -- This will reverse the transition when the board reaches it's final position, i.e. moving the board back to x=0 iterations = 0, -- Repeat forever })

If I may, here’s a slight refactoring:

-- Create the board and position it at the left hand side of the screen local board = display.newRect(0, display.contentCenterY, 100, 20) board.anchorX = 0 -- Declare functions as variables to be able to cross-reference from onComplete functions local goLeft, goRight goLeft = function() transition.to(board, { x = 0, time = 1000, onComplete = goRight }) end goRight = function() transition.to(board, { x = display.contentWidth - board.width, time = 1000, onComplete = goLeft }) end -- Start moving the board to the right goRight()

This works because the forward reference to goLeft and goRight allows the functions to address the as-yet-undefined functions when the code actually runs. The onComplete property of the transition.to parameters object is a function reference.

We did a series of tutorials on this topic in the path. There were three of them:

  1. Moving using transition.to() (tutorial is not available today)

  2. Moving using an enterFrame listener: https://coronalabs.com/blog/2015/08/25/tutorial-animation-with-enterframe-listeners/

  3. Moving using physics (tutorial was unpublished)

I’m not sure why we are missing the two tutorials, but…   The gist of the 3rd option was to use two “.isSensor” invisible objects that define the range of movement you want your object to have and use collision events to determine when you need to change direction. When the collision is detected, you do a setLinearVelocity on the object in the direction you want to go.

All three methods have their pros and cons, and you will need to figure out what works best for you.

Rob

You could also combine the options of display object transitions with physics objects being moved and use a touch joint to control the physics object’s position but setting it’s target location to that of a transitioned display object. (Ask me and I’ll break that explanation down.)

Thanks everybody for helping me out, thanks horacebury.