Changing an image each time in a loop

Hello

I am learning a lot about loops, thanks to all of you.

I using this post, for future reference and for anyone like me

really new to loops.

a basic loop with a “touch” function

    local number = 1     for i = 1, 7 do            nail[i] = display.newImageRect( sceneGroup, "images/nail" .. number .. ".png", 250, 188 )            nail[i].x = display.contentCenterX            nail[i].y = display.contentCenterY + i\*120            nail[i]:scale (.6, .6)            nail[i].isVisible = true            nail[i].name = i            nail[i].touch = touch            nail[i]:addEventListener("touch")            number = number + 1     end

I got this from this post. However, in this loop I have a different image each time

because of the – … number … – I think is “concatenation”

so I got nail1, nail2, nail3 and so on

but if I want to get diferent numbers and add a condicional “if” like this

    if ar.data.note == true then         p1 = display.newImageRect ( sceneGroup, "images/n40.jpg", 106, 106)     end     if ar.data.note == false then         p1 = display.newImageRect ( sceneGroup, "images/a40.jpg", 106, 106)     end

Notice that one is " n40"  and the other one is  “a40”

I have a list of 88 numbers, n52, n47, n08 like that

so far i do this 60 times

    if archivo.datos.doremi == true then         p13 = display.newImageRect ( sceneGroup, "images/n45.jpg", 106, 106)     end     if archivo.datos.doremi == false then         p13 = display.newImageRect ( sceneGroup, "images/a45.jpg", 106, 106)     end     p13.x = pin2[5].x     p13.y = pin2[5].y     p13:scale (1.2, 1.2)

it’s a lot!

QUESTION

If I have 60 “p” variables – p1, p2, p3, p4 …

each of them has a different  image   n52 or a52,   n47 or a47 like that

and also different .x and .y position

How would I do that in a loop for i = 1, 60 do ?

I hope you understand my question thanks.

another way to consider would be to put all details into a table.

[lua]

table = {

{“images/a45.png”, 106, 106,1.2,1.2},

}

p13 = display.newImageRect ( sceneGroup, table[num][1], table[num][2], table[num][3])

[/lua]

kind of thing

Hi ToeKnee … If I write

“images/a45.png” instead of table[num][1],

I still have to write 60 times the variable

p1

p2

p3

p4

p60

it’s a lot of times.

What I am looking for is a loop

for i = 1, 60 do

 – Here Do things

end

This is my basic code

    if archivo.datos.doremi == true then         p1 = display.newImageRect ( sceneGroup, "images/n47.jpg", 106, 106)     end     if archivo.datos.doremi == false then         p1 = display.newImageRect ( sceneGroup, "images/a47.jpg", 106, 106)     end     p1.x = pin[1].x     p1.y = pin[1].y     p1:scale (escalaX, escalaY)

I do this 60 times, a lot!!!

I understand that the loop will do the same command line by line

so I only have to change the things that I want to change

if archivo.datos.doremi == true then – This will stay the same

            p1 = display.newImageRect ( sceneGroup, “images/n47.jpg”, 106, 106)  – Here I need to change p1, p2, p3, p4 and so on

                    also I need to change the image name like n47 or n42 or n52 like that

So I guess I would need some kind of table

table = {

{n47},

{n48},

}

I guess I would need some concatenation

“images/” … table [2] … “.jpg”

But How do I know what number to put base on what?

is kind of confusing I hope someone can help me

you had 

         nail[i] = display.newImageRect( sceneGroup, “images/nail” … number … “.png”, 250, 188 )

within a loop

by replacing - “images/nail” … number … “.png”, 250, 188 with - table[num][1], table[num][2], table[num][3] style

you can loop through your images. Yes you will need a table - it’s your choice whether you use concatenation or not.

T.

I think we are getting a lot of progress here. I reduce the code by almost 300 lines!

I have this simple code

    local tabla = {     [1] = "images/n40.jpg",     [2] = "images/n42.jpg",     [3] = "images/n44.jpg",     [4] = "images/n45.jpg",     [5] = "images/n47.jpg",     [6] = "images/n49.jpg",     [7] = "images/n51.jpg",     [8] = "images/n52.jpg"     }          local table = {     [1] = "images/a40.jpg",     [2] = "images/a42.jpg",     [3] = "images/a44.jpg",     [4] = "images/a45.jpg",     [5] = "images/a47.jpg",     [6] = "images/a49.jpg",     [7] = "images/a51.jpg",     [8] = "images/a52.jpg"     }          local space = 1     for i = 1, 16 do     if archivo.datos.doremi == true then         p1 = display.newImageRect ( sceneGroup, tabla[2], 106, 106)     end     if archivo.datos.doremi == false then         p1 = display.newImageRect ( sceneGroup, table[2], 106, 106)     end     p1.x = pin[space].x     p1.y = pin[space].y     p1:scale (escalaX, escalaY)     space = space + 1     end

I see 16 images if == true i see 1 image

if == false I see another image

and it works fine.

QUESTION

in table[2] i can do the same as this

local space = 1

p1.x = pin[space].x

space = space + 1

but that will show me the images in order ( a40, a42, a44 … )

the problem I have is how I manage to put a different order?

like

local order = “a40”, “a44”, “a40”, “a47”, “a42”

like that?

another way to consider would be to put all details into a table.

[lua]

table = {

{“images/a45.png”, 106, 106,1.2,1.2},

}

p13 = display.newImageRect ( sceneGroup, table[num][1], table[num][2], table[num][3])

[/lua]

kind of thing

Hi ToeKnee … If I write

“images/a45.png” instead of table[num][1],

I still have to write 60 times the variable

p1

p2

p3

p4

p60

it’s a lot of times.

What I am looking for is a loop

for i = 1, 60 do

 – Here Do things

end

This is my basic code

    if archivo.datos.doremi == true then         p1 = display.newImageRect ( sceneGroup, "images/n47.jpg", 106, 106)     end     if archivo.datos.doremi == false then         p1 = display.newImageRect ( sceneGroup, "images/a47.jpg", 106, 106)     end     p1.x = pin[1].x     p1.y = pin[1].y     p1:scale (escalaX, escalaY)

I do this 60 times, a lot!!!

I understand that the loop will do the same command line by line

so I only have to change the things that I want to change

if archivo.datos.doremi == true then – This will stay the same

            p1 = display.newImageRect ( sceneGroup, “images/n47.jpg”, 106, 106)  – Here I need to change p1, p2, p3, p4 and so on

                    also I need to change the image name like n47 or n42 or n52 like that

So I guess I would need some kind of table

table = {

{n47},

{n48},

}

I guess I would need some concatenation

“images/” … table [2] … “.jpg”

But How do I know what number to put base on what?

is kind of confusing I hope someone can help me

you had 

         nail[i] = display.newImageRect( sceneGroup, “images/nail” … number … “.png”, 250, 188 )

within a loop

by replacing - “images/nail” … number … “.png”, 250, 188 with - table[num][1], table[num][2], table[num][3] style

you can loop through your images. Yes you will need a table - it’s your choice whether you use concatenation or not.

T.

I think we are getting a lot of progress here. I reduce the code by almost 300 lines!

I have this simple code

    local tabla = {     [1] = "images/n40.jpg",     [2] = "images/n42.jpg",     [3] = "images/n44.jpg",     [4] = "images/n45.jpg",     [5] = "images/n47.jpg",     [6] = "images/n49.jpg",     [7] = "images/n51.jpg",     [8] = "images/n52.jpg"     }          local table = {     [1] = "images/a40.jpg",     [2] = "images/a42.jpg",     [3] = "images/a44.jpg",     [4] = "images/a45.jpg",     [5] = "images/a47.jpg",     [6] = "images/a49.jpg",     [7] = "images/a51.jpg",     [8] = "images/a52.jpg"     }          local space = 1     for i = 1, 16 do     if archivo.datos.doremi == true then         p1 = display.newImageRect ( sceneGroup, tabla[2], 106, 106)     end     if archivo.datos.doremi == false then         p1 = display.newImageRect ( sceneGroup, table[2], 106, 106)     end     p1.x = pin[space].x     p1.y = pin[space].y     p1:scale (escalaX, escalaY)     space = space + 1     end

I see 16 images if == true i see 1 image

if == false I see another image

and it works fine.

QUESTION

in table[2] i can do the same as this

local space = 1

p1.x = pin[space].x

space = space + 1

but that will show me the images in order ( a40, a42, a44 … )

the problem I have is how I manage to put a different order?

like

local order = “a40”, “a44”, “a40”, “a47”, “a42”

like that?