Mehmet Ait Addi
Mehmet Ait Addi

Reputation: 135

How to concatenate variable with an object name?

I would like to check each value in my table (file), and when the value is set in "1", i would like my function to make the concerning object visible, so i've tried this, but doesn't work :

I've twelve objects, called : "check1", "check2", ... until "check12".

local check12 = display.newImageRect ("greenchecked.png", 70,90)
check12.x = display.contentCenterX+310
check12.y = display.contentCenterY+100
check12.isVisible=false
group:insert(2,check12)

skin_saved[] is my table, where all data are saved. It works, because I can read the table, and data are correct. So, this is my function, but How to concatenate "i" with the object name ?

for i=1,12 do
    if skin_saved[i]==1 then 
        img="check"..i
        print(img)
        i=i+1
        img.isVisible=true 
    end
end

Upvotes: 0

Views: 931

Answers (2)

Rob Miracle
Rob Miracle

Reputation: 3063

I would use a table:

local check = {}

then reference each item by index:

check[12] = display.newImageRect ("greenchecked.png", 70,90)

That seems to be simpler approach.

Upvotes: 1

Bartek Banachewicz
Bartek Banachewicz

Reputation: 39390

img=_G["check"..i]

But don't do that. Use an array and index check[i] instead.

Upvotes: 1

Related Questions