[SOLVED] Trouble using -1 with for loops

This one is driving me crazy. Maybe I’ve mixed up something with the syntax.

Basically, I have a series of sprites I want to move using Runtime and :translate(), like this:

[code]for i = 1, people.numChildren do
local moveX, moveY
local speed = 2
local nextpoint = waypoint + 1 – waypoints are x/y info stored in a table.

– figure out where to move along X, if any
if people[i].x == waypoints[nextpoint].x then
moveX = 0
else if people[i].x > waypoints[nextpoint].x then
moveX = -speed
else if people[i].x < waypoints[nextpoint].x then
moveX = speed
end
– Increment waypoint if necessary
if moveX == 0 then
waypoint = waypoint + 1
else
people[i]:translate(moveX,0)
end

end
Runtime:addEventListener(“enterFrame”, theFunctionthatdoesthestuffabove)
[/code]

Seems clean. Works perfectly. But if I add a -1 to the for loop…

for i = 1, people.numChildren, -1 do

The first person moves to somewhere between 40 and 50 px, stops, and that’s it, the whole for loop is over as if I hit an assert. Even though the for loop is set to take place on enterFrame, the function is firing but the for loop is not.

Case in point, add a print statement immediately after the for loop:

for i = 1, shoppers.numChildren, -1 do print("The reverse for loop is working!") end

…and the print statement ceases exactly as the character does.

Am I using reverse loops the wrong way? [import]uid: 41884 topic_id: 24520 reply_id: 324520[/import]

Try:

for i = people.numChildren, 1, -1 do

and

for i = shoppers.numChildren, 1, -1 do

Naomi

[import]uid: 67217 topic_id: 24520 reply_id: 99251[/import]

Yeah. That did it. For some reason I was convinced 1 always had to come first in the format for the for loop. >_< Thanks Naomi! :slight_smile: [import]uid: 41884 topic_id: 24520 reply_id: 99255[/import]

You’re so welcome.

Cheers,
Naomi [import]uid: 67217 topic_id: 24520 reply_id: 99257[/import]