[RESOLVED] programming problem, please help!

<<Solved>>

Dear All,

Need help!

I have try to build some boxes when you touch it will disappear.(first part of my code)

Also there are some other button can change the color of above boxes.(2nd part of my code)

My problem is when I build this boxes with for loop it can interact with their own event listener. But it cannot reference with the second event listener. The error keep saying there are nil value at this line “selectBtn[1]:setFillColor(255,255,255)”. 

I have a work around solution. If I give up to use for loop to build this boxes. Then it can recognized by other event listener. I’m planning to build hundred of this boxes. I must use for loop. Can anyone know the way how to use this for loop in a smarter way.

Like this build each boxes one by one.

            local lineBtn1 = display.newRect(130,290,30,6)

            lineBtn1:setFillColor( 111,111,111)

            lineBtn1.id = 6

            controlGroup:insert(lineBtn1)

            local lineBtn2 = display.newRect(160,290,30,9)

            lineBtn2:setFillColor( 111,111,111)

            lineBtn2.id = 9

            controlGroup:insert(lineBtn2)

            

            local lineBtn3 = display.newRect(190,290,30,12)

            lineBtn3:setFillColor( 111,111,111)

            lineBtn3.id = 12

            controlGroup:insert(lineBtn3)

            

            local lineBtn4 = display.newRect(220,290,30,15)

            lineBtn4:setFillColor( 111,111,111)

            lineBtn4.id = 15

            controlGroup:insert(lineBtn4)

            …

local selectBtn = {} local colourBtn = {} xline = 100 for p = 1,10 do xline = xline + boxWidth yline = 260 yline = yline + boxHeight local function selectbtnFunc(event) local obj = event.target if event.phase == "ended" then \<\<do something here\>\> obj.alpha = 0 end return true end selectBtn[p] = display.newRect(xline,yline,boxWidth,boxHeight) selectBtn[p]:setFillColor(0,0,0) selectBtn[p].id = p selectBtn[p].alpha = 1 controlGroup:insert(selectBtn[p]) selectBtn[p]:addEventListener("touch", selectbtnFunc) end local colourNum = 1 x = 350 for l = 1,3 do x = x + boxWidth y = -25 for m = 1,10 do y = y + boxHeight local function selectcolour(event) local lineobj = event.target print(lineobj.id) linesetup = lineobj.id if event.phase == "ended" then \<\<do something here\>\> \<\<Try to change the select button colour\>\> selectBtn[1]:setFillColor(255,255,255) selectBtn[2]:setFillColor(255,255,255) selectBtn[3]:setFillColor(255,255,255) selectBtn[4]:setFillColor(255,255,255) selectBtn[5]:setFillColor(255,255,255) selectBtn[6]:setFillColor(255,255,255) selectBtn[7]:setFillColor(255,255,255) selectBtn[8]:setFillColor(255,255,255) selectBtn[9]:setFillColor(255,255,255) selectBtn[10]:setFillColor(255,255,255) end return true end colourBtn[colourNum] = display.newRect(xline,yline,boxWidth,boxHeight) colourBtn[colourNum]:setFillColor(tableR,tableG,tableB) colourBtn[colourNum].id = colourNum colourBtn[colourNum].alpha = 1 controlGroup:insert(colourBtn[colourNum]) colourBtn[colourNum]:addEventListener("touch", selectcolour) colourNum =colourNum + 1 end end

At a quick glance, your colourNum variable is not inside a for loop.  Therefore it is always equal to 1.

Dear JonPM,

The colourNum was there, I only missing the “local” when I do copy and paste.

Both for loop is running and they can bulid the object on screen and the touch event are ok too. Except for the event of ColourBtn can not reference the selectBtn with brackets. 

I saw other post to solve this is using a parent to group this two object. But I have no idea about how to use a parent. Is there any other way to do it.

Thanks

KC

Dear All, 

I have solved this problem by added a table selectcontainer to contain the selectBtn.

Now I can reference using a Brackets selectBtn[1] to change anythings on it.

Thanks anyway.

KC

 local selectcontainer = {} local selectBtn = {} local colourBtn = {} xline = 100 for p = 1,10 do xline = xline + boxWidth yline = 260 yline = yline + boxHeight local function selectbtnFunc(event) local obj = event.target if event.phase == "ended" then \<\<do something here\>\> obj.alpha = 0 end return true end selectcontainer = selectBtn[p] selectBtn[p] = display.newRect(xline,yline,boxWidth,boxHeight) selectBtn[p]:setFillColor(0,0,0) selectBtn[p].id = p selectBtn[p].alpha = 1 controlGroup:insert(selectBtn[p]) selectBtn[p]:addEventListener("touch", selectbtnFunc) end local colourNum = 1 x = 350 for l = 1,3 do x = x + boxWidth y = -25 for m = 1,10 do y = y + boxHeight local function selectcolour(event) local lineobj = event.target print(lineobj.id) linesetup = lineobj.id if event.phase == "ended" then \<\<do something here\>\> \<\<Try to change the select button colour\>\> selectBtn[1]:setFillColor(255,255,255) selectBtn[2]:setFillColor(255,255,255) selectBtn[3]:setFillColor(255,255,255) selectBtn[4]:setFillColor(255,255,255) selectBtn[5]:setFillColor(255,255,255) selectBtn[6]:setFillColor(255,255,255) selectBtn[7]:setFillColor(255,255,255) selectBtn[8]:setFillColor(255,255,255) selectBtn[9]:setFillColor(255,255,255) selectBtn[10]:setFillColor(255,255,255) end return true end colourBtn[colourNum] = display.newRect(xline,yline,boxWidth,boxHeight) colourBtn[colourNum]:setFillColor(tableR,tableG,tableB) colourBtn[colourNum].id = colourNum colourBtn[colourNum].alpha = 1 controlGroup:insert(colourBtn[colourNum]) colourBtn[colourNum]:addEventListener("touch", selectcolour) colourNum =colourNum + 1 end end

At a quick glance, your colourNum variable is not inside a for loop.  Therefore it is always equal to 1.

Dear JonPM,

The colourNum was there, I only missing the “local” when I do copy and paste.

Both for loop is running and they can bulid the object on screen and the touch event are ok too. Except for the event of ColourBtn can not reference the selectBtn with brackets. 

I saw other post to solve this is using a parent to group this two object. But I have no idea about how to use a parent. Is there any other way to do it.

Thanks

KC

Dear All, 

I have solved this problem by added a table selectcontainer to contain the selectBtn.

Now I can reference using a Brackets selectBtn[1] to change anythings on it.

Thanks anyway.

KC

 local selectcontainer = {} local selectBtn = {} local colourBtn = {} xline = 100 for p = 1,10 do xline = xline + boxWidth yline = 260 yline = yline + boxHeight local function selectbtnFunc(event) local obj = event.target if event.phase == "ended" then \<\<do something here\>\> obj.alpha = 0 end return true end selectcontainer = selectBtn[p] selectBtn[p] = display.newRect(xline,yline,boxWidth,boxHeight) selectBtn[p]:setFillColor(0,0,0) selectBtn[p].id = p selectBtn[p].alpha = 1 controlGroup:insert(selectBtn[p]) selectBtn[p]:addEventListener("touch", selectbtnFunc) end local colourNum = 1 x = 350 for l = 1,3 do x = x + boxWidth y = -25 for m = 1,10 do y = y + boxHeight local function selectcolour(event) local lineobj = event.target print(lineobj.id) linesetup = lineobj.id if event.phase == "ended" then \<\<do something here\>\> \<\<Try to change the select button colour\>\> selectBtn[1]:setFillColor(255,255,255) selectBtn[2]:setFillColor(255,255,255) selectBtn[3]:setFillColor(255,255,255) selectBtn[4]:setFillColor(255,255,255) selectBtn[5]:setFillColor(255,255,255) selectBtn[6]:setFillColor(255,255,255) selectBtn[7]:setFillColor(255,255,255) selectBtn[8]:setFillColor(255,255,255) selectBtn[9]:setFillColor(255,255,255) selectBtn[10]:setFillColor(255,255,255) end return true end colourBtn[colourNum] = display.newRect(xline,yline,boxWidth,boxHeight) colourBtn[colourNum]:setFillColor(tableR,tableG,tableB) colourBtn[colourNum].id = colourNum colourBtn[colourNum].alpha = 1 controlGroup:insert(colourBtn[colourNum]) colourBtn[colourNum]:addEventListener("touch", selectcolour) colourNum =colourNum + 1 end end