best way to manage spanw object inside an array in local function

Hi

i am trying to spawn lots of objects same type same name just increment the name with 1 .

now how can i know exactly which object had been spawn and still on the screen and how can i access each one of them  outside their function

see my code down

local Spawn5 = function () Object5= {} i=0 for i= 1,1 do Object5[i] = display.newSprite( sheet\_Object5, sequences\_Object5 ); Object5[i].y= 0 Object5[i].x = (math.random(screenLeft+10, screenRight-20)) sceneGroup:insert(Object5[i]) physics.addBody( Orange5Parts[i], "dynamic" ,{radius = 0, isSensor = true }) transition.to ( Orange5Parts[i], {time=5000,x=150,y=700,tag = "animationBlock" } ) Object5[i]:addEventListener( "collision", Object5Colide ) return Object5[i] end end

I would change the scope of Object5 and add an Id field… something like this.

local Object5= {} local Spawn5 = function () for i= 1,1 do Object5[i] = display.newSprite( sheet\_Object5, sequences\_Object5 ); Object5[i].y= 0 Object5[i].x = (math.random(screenLeft+10, screenRight-20)) Object5[i].id = i sceneGroup:insert(Object5[i]) physics.addBody( Orange5Parts[i], "dynamic" ,{radius = 0, isSensor = true }) transition.to ( Orange5Parts[i], {time=5000,x=150,y=700,tag = "animationBlock" } ) Object5[i]:addEventListener( "collision", Object5Colide ) return Object5[i] end end

This way, you can refer to the id of the object anywhere in your scene (assuming you are using composer).

Sounds like you want to create and manage your objects via object pooling.  I wrote an article about that here.  I think it may help you.

–John

Thanks for your reply

but that didn’t help

well let me explain more simple .

i need to spawn 5 objects every object have to have unique ID and name >> i already did that

and every object have to be spawned every 10 seconds from the same function 

see my code till now

local Spawn5 = function () Object5= {} i=0 for i= 1,1 do Object5[i] = display.newSprite( sheet\_Object5, sequences\_Object5 ); Object5[i].y= 0 Object5[i].x = (math.random(screenLeft+10, screenRight-20)) sceneGroup:insert(Object5[i]) Object5[i].id = i Object5[i].name = ("Object5"..i); physics.addBody( Orange5Parts[i], "dynamic" ,{radius = 0, isSensor = true }) transition.to ( Orange5Parts[i], {time=5000,x=150,y=700,tag = "animationBlock" } ) Object5[i]:addEventListener( "collision", Object5Colide ) end end

the above code spwan all the 5 objects same time same Y location , and i want only one object to be spawned each 10 seconds with unique ID and name

i can do that manually by creating each object but i want that to be automatic using a function because later i would like to create more than 5 .

I think I understand.

If you want to call your spawn function every x seconds, take a look at timer.performWithDelay().  For every 10 seconds, you can do this:

timer.performWithDelay(10000, Spawn5, -1)

In your code above, the for loop will only execute once:

for i= 1,1 do

thanks schizoid2k for your reply

i already have that in my code

ControlTimer= timer.performWithDelay(2000, Spawn5,5 )

but that will always spawn 1 object for 5 times . same Name and i wil not be able to differentiate between them

sorry for the above code i can see now why you didnot get it

i should add for

for i= 1,5 do

but that will create 5 object same moment and i want to delay creating these object but still have 5 different names

like

Object51

Object52

Object53

Object54

Object55

and in this case i con control these later

what do you think ?

Sorry for my confusion… I hope I am not making things worse  :slight_smile: .

So I think I understand… you want to spawn 5 objects over a total time of 10 seconds?  If that is right, I would just keep a counter and increment that to create an unique name:

local Object5= {} local counter = 0 local Spawn5 = function () for i= 1,5 do counter = counter + 1 Object5[i] = display.newSprite( sheet\_Object5, sequences\_Object5 ); Object5[i].y = 0 Object5[i].x = (math.random(screenLeft+10, screenRight-20)) sceneGroup:insert(Object5[i]) Object5[i].id = counter Object5[i].name = ("Object5"..counter); physics.addBody( Orange5Parts[i], "dynamic" ,{radius = 0, isSensor = true }) transition.to ( Orange5Parts[i], {time=5000,x=150,y=700,tag = "animationBlock" } ) Object5[i]:addEventListener( "collision", Object5Colide ) end end ControlTimer= timer.performWithDelay(2000, Spawn5, 5)

This should do the trick.  Create and destroy these objects as needed.

If you notice any performance issues creating and destroying objects over and over again, I would recommend reading the article I linked to above.  This will allow you to create a bunch of objects in a pool, assign physics properties to them, and use them over and over.

I hope this helps.  If I still do not understand (hey, it could happen!), please let me know.

–john

schizoid2k thanks again

it works but i have one issue now . i have a button that when tapped it should kill these object5 one by one .

both of these functions are in the create scene and i already tried to make object5 global by removing the ( local ) word . but still i cant access those object created above

 local function PressKill5Button ( event ) print(Orange5Parts[i]) if Object5[i] ~= nil then Object5[i]:removeSelf() elseif Object5[i]==nil then end end KillButton:addEventListener( "tap", PressKill5Button )

the above code didn’t able to access the Object5  , the above function is after the spawn function

I would not make Object5 global.  You can keep it local, but above the functions.  That makes the variable local to the scene, which is what you want.

To remove all objects, try this…

local function PressKill5Button(event) for i = #Object5, 1, -1 do display.remove(Object5[i]) end end KillButton:addEventListener( "tap", PressKill5Button )

display.remove first checks if the object is not nil.

I also like to remove objects in an array in reverse order for performance reasons.

–john

it works but it remove only the last object that had be spawn

for example if there are 3 objects spawn and i pres the Kill button it will remove only the last object and will not remove the first 2 spawned

local function PressKill5Button(event) for i = #Object5, 1, -1 do display.remove(Object5[i]) Object5[i] = nil end end KillButton:addEventListener( "tap", PressKill5Button )

Thanks . but that gave me an err

that attempt to index a field (?) a nil value . actually it cant even print Object5[i]

The code below adds and then removes 5 display objects into an array.  If this is not happening on your end, there must be something else going on… perhaps a scope issue?  A missing end?  A second button tap would probably cause that error as well since the Object5 array no longer exists.

local Object5 = {} local function PressKill5Button() for i = #Object5, 1, -1 do display.remove(Object5[i]) Object5[i] = nil end end for i = 1,5 do Object5[i] = display.newImage("myImage.png") end PressKill5Button()

I would change the scope of Object5 and add an Id field… something like this.

local Object5= {} local Spawn5 = function () for i= 1,1 do Object5[i] = display.newSprite( sheet\_Object5, sequences\_Object5 ); Object5[i].y= 0 Object5[i].x = (math.random(screenLeft+10, screenRight-20)) Object5[i].id = i sceneGroup:insert(Object5[i]) physics.addBody( Orange5Parts[i], "dynamic" ,{radius = 0, isSensor = true }) transition.to ( Orange5Parts[i], {time=5000,x=150,y=700,tag = "animationBlock" } ) Object5[i]:addEventListener( "collision", Object5Colide ) return Object5[i] end end

This way, you can refer to the id of the object anywhere in your scene (assuming you are using composer).

Sounds like you want to create and manage your objects via object pooling.  I wrote an article about that here.  I think it may help you.

–John

Thanks for your reply

but that didn’t help

well let me explain more simple .

i need to spawn 5 objects every object have to have unique ID and name >> i already did that

and every object have to be spawned every 10 seconds from the same function 

see my code till now

local Spawn5 = function () Object5= {} i=0 for i= 1,1 do Object5[i] = display.newSprite( sheet\_Object5, sequences\_Object5 ); Object5[i].y= 0 Object5[i].x = (math.random(screenLeft+10, screenRight-20)) sceneGroup:insert(Object5[i]) Object5[i].id = i Object5[i].name = ("Object5"..i); physics.addBody( Orange5Parts[i], "dynamic" ,{radius = 0, isSensor = true }) transition.to ( Orange5Parts[i], {time=5000,x=150,y=700,tag = "animationBlock" } ) Object5[i]:addEventListener( "collision", Object5Colide ) end end

the above code spwan all the 5 objects same time same Y location , and i want only one object to be spawned each 10 seconds with unique ID and name

i can do that manually by creating each object but i want that to be automatic using a function because later i would like to create more than 5 .

I think I understand.

If you want to call your spawn function every x seconds, take a look at timer.performWithDelay().  For every 10 seconds, you can do this:

timer.performWithDelay(10000, Spawn5, -1)

In your code above, the for loop will only execute once:

for i= 1,1 do

thanks schizoid2k for your reply

i already have that in my code

ControlTimer= timer.performWithDelay(2000, Spawn5,5 )

but that will always spawn 1 object for 5 times . same Name and i wil not be able to differentiate between them

sorry for the above code i can see now why you didnot get it

i should add for

for i= 1,5 do

but that will create 5 object same moment and i want to delay creating these object but still have 5 different names

like

Object51

Object52

Object53

Object54

Object55

and in this case i con control these later

what do you think ?

Sorry for my confusion… I hope I am not making things worse  :slight_smile: .

So I think I understand… you want to spawn 5 objects over a total time of 10 seconds?  If that is right, I would just keep a counter and increment that to create an unique name:

local Object5= {} local counter = 0 local Spawn5 = function () for i= 1,5 do counter = counter + 1 Object5[i] = display.newSprite( sheet\_Object5, sequences\_Object5 ); Object5[i].y = 0 Object5[i].x = (math.random(screenLeft+10, screenRight-20)) sceneGroup:insert(Object5[i]) Object5[i].id = counter Object5[i].name = ("Object5"..counter); physics.addBody( Orange5Parts[i], "dynamic" ,{radius = 0, isSensor = true }) transition.to ( Orange5Parts[i], {time=5000,x=150,y=700,tag = "animationBlock" } ) Object5[i]:addEventListener( "collision", Object5Colide ) end end ControlTimer= timer.performWithDelay(2000, Spawn5, 5)

This should do the trick.  Create and destroy these objects as needed.

If you notice any performance issues creating and destroying objects over and over again, I would recommend reading the article I linked to above.  This will allow you to create a bunch of objects in a pool, assign physics properties to them, and use them over and over.

I hope this helps.  If I still do not understand (hey, it could happen!), please let me know.

–john

schizoid2k thanks again

it works but i have one issue now . i have a button that when tapped it should kill these object5 one by one .

both of these functions are in the create scene and i already tried to make object5 global by removing the ( local ) word . but still i cant access those object created above

 local function PressKill5Button ( event ) print(Orange5Parts[i]) if Object5[i] ~= nil then Object5[i]:removeSelf() elseif Object5[i]==nil then end end KillButton:addEventListener( "tap", PressKill5Button )

the above code didn’t able to access the Object5  , the above function is after the spawn function

I would not make Object5 global.  You can keep it local, but above the functions.  That makes the variable local to the scene, which is what you want.

To remove all objects, try this…

local function PressKill5Button(event) for i = #Object5, 1, -1 do display.remove(Object5[i]) end end KillButton:addEventListener( "tap", PressKill5Button )

display.remove first checks if the object is not nil.

I also like to remove objects in an array in reverse order for performance reasons.

–john

it works but it remove only the last object that had be spawn

for example if there are 3 objects spawn and i pres the Kill button it will remove only the last object and will not remove the first 2 spawned