Block movement back and forth

Still a newbie, tried looking up references to this, but couldn’t find so apologies in advance!

I’m looking to have a block move back and forth on the screen, so if it’s position is < or = to say, 40, change velocity to move right according to game.speed

Then the other way, if it’s position is > or = to 600, change the velocity to the left, so it’s moving back and forth on screen between those parameters

What would be the best way to implement this?

Thank you!

-Jenny [import]uid: 63661 topic_id: 12118 reply_id: 312118[/import]

here’s how i have it in one of my projects:
[lua]object = display.newRect( 0, 0, 50, 50)
object.x = 100

function goLeft ()
transition.to( object, { time=1500, x=(object.x - 50), onComplete=goRight } )
end

function goRight ()
transition.to( object, { time=1500, x=(object.x + 50), onComplete=goLeft } )
end

goLeft()[/lua]

or for an image:

[lua]object = display.newImage( “little.png” )
object.x = 160; object.y = 95

function goLeft ()
transition.to( object, { time=1500, x=(object.x - 50), onComplete=goRight } )
end

function goRight ()
transition.to( object, { time=1500, x=(object.x + 50), onComplete=goLeft } )
end

goLeft()[/lua]

got that code from “slimboyfat” [import]uid: 67514 topic_id: 12118 reply_id: 44105[/import]

Thank you so much!!

Appreciate your help! [import]uid: 63661 topic_id: 12118 reply_id: 44132[/import]

Thank you for making this easy to understand.

iv searched for 2 hours through docs, tutorials, google just for this little awsome snippet of code.

[import]uid: 24981 topic_id: 12118 reply_id: 45506[/import]

Dude that is so 1337.
This code was VERY helpful, I was wondering how to do this.

I have a folder I call “Code Sets” which is a master lua file with every example I come across. I then make it generic and put things like or

etc.

As long as you name things standard like on this one “Back and forth object movement” it’s easy to find :slight_smile:

Thanks again for the code snippet, this is perfect!

-Nick G [import]uid: 61600 topic_id: 12118 reply_id: 47767[/import]