Variables Names

I am trying to consolidate my code with loops.

  
 function renew()  
 for i=1, 5, 1 do  
 Rect[i]:setFillColor(100,170,180)  
 end  
 end  

The problem is when i try the set the color of the I in the third line of the code.

Im not sure how to do this.
Thanks
[import]uid: 24708 topic_id: 25685 reply_id: 325685[/import]

You aren’t giving us quite enough info here about the error but my guess is perhaps Rect isn’t a table? Here is one example of how this would work;
[lua]local Rect = {}

for i = 1, 5 do
Rect[i] = display.newRect( i*20, i*20, 20, 20 )
end

local function renew()
for i = 1, 5 do
Rect[i]:setFillColor(255, 0, 0)
end
end
renew()[/lua]
(Keep in mind you could use #Rect rather than 5 on line 8)

Or if you aren’t creating them in a loop you could do;
[lua]local Rect = {}

Rect[1] = display.newRect( 20, 20, 20, 20 )
Rect[2] = display.newRect( 90, 200, 20, 25 )
Rect[3] = display.newRect( 220, 300, 30, 20 )

local function renew()
for i = 1, #Rect do
Rect[i]:setFillColor(255, 0, 0)
end
end
renew()[/lua]
(In that second example I use #Rect)

I hope this helps :slight_smile:

Peach

[import]uid: 52491 topic_id: 25685 reply_id: 103850[/import]

I was originally using independent Rects, but ill try to set it up using a *table* like your second example.

Just out of curiosity what would you have to do if it wasn’t a table?
Thanks [import]uid: 24708 topic_id: 25685 reply_id: 103851[/import]

Well you need to use a table if you’re using a for loop like above because otherwise there would be nothing to iterate through.

If it wasn’t a table you’d have to set the colors one at a time.
Eg;
rect1:setFillColor(255,0,0)
rect2:setFillColor(255,0,0)
rect3:setFillColor(255,0,0)

etc. [import]uid: 52491 topic_id: 25685 reply_id: 103868[/import]

Great post, Peach! [import]uid: 19626 topic_id: 25685 reply_id: 103873[/import]