Help on for ... i loop

Hi everyone…

Something strange in a loop

I have 10 images, 1 to 10

I would like to display the images one on top of another, like pages of a book

but I want to see No. 1 First and then 2 so 10 will be in the back.

I’m trying to learn to use for i = 1, 10, do – so I have this

    for i = 10, 1 do         -- Leccion No. 1         bg[i] = display.newImageRect( sceneGroup, "lessons/l" .. 11-i .. ".jpg", 600, 400 )         bg[i].x = display.contentCenterX         bg[i].y = display.contentCenterY         bg[i].isVisible = true     end

But I see NO images at all in the App


If I do this… I CAN see the images

    for i = 1, 10 do         -- Leccion No. 1         bg[i] = display.newImageRect( sceneGroup, "lessons/l" .. i .. ".jpg", 600, 400 )         bg[i].x = display.contentCenterX         bg[i].y = display.contentCenterY         bg[i].isVisible = true     end

But I see No. 10 on top


I also try this one

      local number = 1     for i = 1, 10 do         -- Leccion No. 1         bg[i] = display.newImageRect( sceneGroup, "lessons/l" .. number .. ".jpg", 600, 400 )         bg[i].x = display.contentCenterX         bg[i].y = display.contentCenterY         bg[i].isVisible = true         number = number+1     end

I also SEE No. 10 on top

So how would I do it to see No. 1 on top, then No. 2, No 3 and so on…?

Thanks for all your help

Hi @helloworld2013d,

The default increment for Lua loops is 1 (+1), so if you want to loop backwards from 10 to 1, you need to specify the increment like this:

[lua]

for i = 10, 1, -1 do

[/lua]

Note the “-1” as the third part of that.

Take care,

Brent

Hi @helloworld2013d,

The default increment for Lua loops is 1 (+1), so if you want to loop backwards from 10 to 1, you need to specify the increment like this:

[lua]

for i = 10, 1, -1 do

[/lua]

Note the “-1” as the third part of that.

Take care,

Brent