remove multi objects

I use a for loop to creat multi Circle

for i = 1,10 do
	local myCircle = display.newCircle(150 +i,50+i*50,30)
	myCircle:setStrokeColor( 1, 0, 0 )
	sceneGroup:insert(myCircle)	
end

so sceneGroup have 1 child call “myCircle” right ?
in "myCircle there are 10 Circle
but I don;t know how to edit their properties
E.G hide first 5 of them
colour it different

You need to do some reading regarding object scope…

local circles = {}

--make them and assign a reference to a table for use later on
for i = 1, 10 do
	circles[i] = display.newCircle(150 +i,50+i*50,30)
	circles[i]:setStrokeColor( 1, 0, 0 )
	sceneGroup:insert(circle[i])	
end

--later on remove them
for i = 10, 1, -1 do
	display.remove(circles[i])
	circles[i] = nil	
end
1 Like

To elaborate on SGS’s answer a bit.

The keyword ‘local’ means: this name will only be recognised within this ‘block’ of code. In your case that would be everything within the for loop (from the line starting ‘for’ until the word ‘end’).

If you try to refer to an object called myCircle outside of that loop, it will not exist. The objects themselves exist as part of the sceneGroup, but they would not have name that you could reference directly.

Similarly, the loop repeats 10 times and each time it creates an object called myCircle. Each instance of that name only exists during that particular loop.
So when i = 1, you create an object called myCircle. When i increases to 2 and the loop repeats, the previous name myCircle is no longer in scope.

In SGS’s example he creates a table before the loop. Then in the loop itself, he populates 10 elements in that table with newCircle objects. These can then be referenced directly outside of the loop using circles[1] to circles[10].

He then removes the circles by looping through that table and deleting them one by one.

Tables and object scope are some of the most basic things that you need to understand before you can make any real progress in Lua. If you haven’t done so already, I highly recommend going through the sample projects and looking up some beginner guides online, otherwise you will find it much harder to do the things you would like to.

1 Like

just start self leaning lua,
use to code in vbs, html
not familiar to how object works in lua

It is great that solar2d community open to new comers :grinning:
Also by asking dumb questions that will keep it in internet so people can seach for it and don’t ask it again. :stuck_out_tongue_winking_eye:

PS:
local circles = {}
if someone is using this code just delete the s on top

1 Like

That is bad advice… note the post has been edited.

1 Like