How to make train on railroad tracks?

I have a line (my tracks) and a train (right now it’s just and arrow). How do I make it so the arrow can ride along the line and follow its path? Also, the path is made with this code -

local outline = display.newRect( 15, 15, 290, 450 )
outline:setFillColor( 0, 0, 0 )
outline:setStrokeColor( 25, 0, 250 )
outline.strokeWidth = 2

Should I be doing it differently? [import]uid: 39628 topic_id: 7455 reply_id: 307455[/import]

Take a look at http://developer.anscamobile.com/code/bezier-object-along-curves-path

You should be able to discern. If not, sure some members here can help out as well.

create your track anyway you want, then setup a series of curves that the “object” can move along to.

or setup a line and interpolate between points. the line(curve) can be invisible and centered around the tracks and the train also centered around the line.

only if there were more hours a day.

for the curve, increase the tolerance for finer grain, decrease for coarse. (that would be the T value)

C. [import]uid: 24 topic_id: 7455 reply_id: 26376[/import]

or you could set up your track as a map of tiles and use per-tile movement calculations (this could also use the bezier calculations, but you’d just repeat functions) [import]uid: 6645 topic_id: 7455 reply_id: 26383[/import]

Years ago I wrote a fun little puzzle game that had a train running along the tracks:
http://www.newarteest.com/games/rrailroad.html

How I did it was pretty simple. First I had a table (well an array, it wasn’t Lua) with all the coordinates of the path listed in it. What I mean is the x,y position of each intersection listed in the order that the train will pass through them.

Then I had a variable to keep track of the train’s current target. That is, which entry in the table the train was currently moving towards.

Finally, I setup an event listener that moved the train toward its target every frame. If the train is below the target add 1 to the train’s Y position, if the train is above the target subtract 1 from the train’s Y position, etc.

That listener would check how close the train was to its target, and if the target was reached the target variable would increment to the next target.

Maybe I should port that game to iPhone. hm… [import]uid: 12108 topic_id: 7455 reply_id: 26393[/import]