Help with "spawning objects the right way"

So I’ve been trying to go through learningcorona.com and stumbled upon “How to spawn objects the right way” at

http://blog.anscamobile.com/2011/09/how-to-spawn-objects-—-the-right-way/
I feel like I’m understanding it pretty well and that it’s creating the objects properly as confirmed by the function

for i = 1, #spawnTable do  
 print(spawnTable[i].myName)  
end  

,but how to I actually change where the objects appear on screen? As you can see, I tried passing in object.x and object.y parameters, but the objects never seem to draw on screen. Any help?

[code]
–Function to spawn an object
local function spawn(params)
local object = display.newImage(params.image)
–Set the objects table to a table passed in by parameters
object.objTable = params.objTable
–Automatically set the table index to be inserted into the next available table index
object.index = #object.objTable + 1

–Give the object a custom name
object.name = “bouncer” … object.index

–If the object should have a body create it, else dont.
if params.hasBody then
–Allow physics parameters to be passed by parameters:
object.density = params.density or 0
object.friction = params.friction or 0
object.bounce = params.bounce or 0
object.isSensor = params.isSensor or false
object.bodyType = params.bodyType or “static”

physics.addBody(object, object.bodyType, {density = object.density, friction = object.friction, bounce = object.bounce, isSensor = object.isSensor})
end

–The objects group
object.group = params.group or nil

–If the function call has a parameter named group then insert it into the specified group
object.group:insert(object)

–Insert the object into the table at the specified index
object.objTable[object.index] = object

object.x = params.x or 50
object.y = params.y or 50

return object
end

–Create a table to hold our spawns
local spawnTable = {}

–Create 50 spawns
for i = 1, 50 do
local spawns = spawn(
{
image = “images/bouncer.png”,
objTable = spawnTable,
hasBody = true,
isSensor = true,
group = localGroup,
x = 100,
y = 100
}
)
end
[/code] [import]uid: 131400 topic_id: 23655 reply_id: 323655[/import]

This is one way, needed the physics decs at the beginning as well as your group creation needed added…

physics = require("physics")  
  
physics.start()  
  
--Function to spawn an object  
local function spawn(params)  
 local object = display.newImage(params.image)  
 --Set the objects table to a table passed in by parameters  
 object.objTable = params.objTable  
 --Automatically set the table index to be inserted into the next available table index  
 object.index = #object.objTable + 1  
  
 --Give the object a custom name  
 object.name = "bouncer" .. object.index  
  
 --If the object should have a body create it, else dont.  
 if params.hasBody then  
 --Allow physics parameters to be passed by parameters:  
 object.density = params.density or 1  
 object.friction = params.friction or 0  
 object.bounce = params.bounce or 0  
 object.isSensor = params.isSensor or false  
 object.bodyType = params.bodyType or "dynamic"  
  
 physics.addBody(object, object.bodyType, {density = object.density, friction = object.friction, bounce = object.bounce, isSensor = object.isSensor})  
 end  
  
 --The objects group  
 object.group = params.group or display.newGroup()  
  
 --If the function call has a parameter named group then insert it into the specified group  
 object.group:insert(object)  
  
 --Insert the object into the table at the specified index  
 object.objTable[object.index] = object  
  
 object.x = params.x or 50  
 object.y = params.y or 50  
  
 return object  
end  
   
--Create a table to hold our spawns  
local spawnTable = {}  
   
--Create 50 spawns  
for i = 1, 50 do  
 local spawns = spawn(  
 {  
 image = "myimage.png",  
 objTable = spawnTable,  
 hasBody = true,  
 isSensor = true,  
 group = localGroup,  
 x = 100 + (i \* 10),  
 y = 100  
 }  
 )  
end  

also changed to dynamic so you could see it react
and threw in a little x increment so you could see all your objects. :slight_smile:

you could also declare:
local localGroup = display.newGroup() at the top of your code if you wanted to keep the " or nil" stuff. :slight_smile: [import]uid: 21331 topic_id: 23655 reply_id: 95008[/import]

Thanks! How would I go about passing in a LinearVelocity through parameters? It gives me an “expected table” error. [import]uid: 131400 topic_id: 23655 reply_id: 95032[/import]

There are so many ways to get these things done, I can’t say that I would keep load copies of the elements velocity since it normally changes due to friction and other physics interactions, but keeping in the style of what you’re doing:

physics = require("physics")  
   
Random = math.random  
  
physics.start()  
   
--Function to spawn an object  
local function spawn(params)  
 local object = display.newImage(params.image)  
 --Set the objects table to a table passed in by parameters  
 object.objTable = params.objTable  
 --Automatically set the table index to be inserted into the next available table index  
 object.index = #object.objTable + 1  
  
 --Give the object a custom name  
 object.name = "bouncer" .. object.index  
  
 --If the object should have a body create it, else dont.  
 if params.hasBody then  
 --Allow physics parameters to be passed by parameters:  
 object.density = params.density or 1  
 object.friction = params.friction or 0  
 object.bounce = params.bounce or 0  
 object.isSensor = params.isSensor or false  
 object.bodyType = params.bodyType or "dynamic"  
  
 physics.addBody(object, object.bodyType, {density = object.density, friction = object.friction, bounce = object.bounce, isSensor = object.isSensor})  
 end  
  
 --The objects group  
 object.group = params.group or display.newGroup()  
  
 --If the function call has a parameter named group then insert it into the specified group  
 object.group:insert(object)  
  
 --Insert the object into the table at the specified index  
 object.objTable[object.index] = object   
 object.x = params.x or 50  
 object.y = params.y or 50  
 object.linearvel = params.linearvel  
  
 object:setLinearVelocity(object.linearvel.x, object.linearvel.y)  
  
 return object  
end  
   
--Create a table to hold our spawns  
local spawnTable = {}  
   
--Create 50 spawns  
for i = 1, 50 do  
 local spawns = spawn(  
 {  
 image = "myimage.png",  
 objTable = spawnTable,  
 hasBody = true,  
 isSensor = true,  
 group = localGroup,  
 x = 100 + (i \* 10),  
 y = 100,  
 linearvel = {x=Random(-40,40), y=Random(-40,40)}  
 }  
 )  
end  

Hope this has helped you along and hope to see you as a subscriber soon! Getting the daily builds is awsome! [import]uid: 21331 topic_id: 23655 reply_id: 95113[/import]

Thanks! Would adding collision event listeners be essentially the same method? I’m at work and can’t check from here :stuck_out_tongue: [import]uid: 131400 topic_id: 23655 reply_id: 95254[/import]

@dustin : Glad you enjoyed the article, and for adding listeners, you could do something like

--Create 50 spawns for i = 1, 50 do local spawns = spawn( { image = "myimage.png", objTable = spawnTable, hasBody = true, isSensor = true, group = localGroup, x = 100 + (i \* 10), y = 100, linearvel = {x=Random(-40,40), y=Random(-40,40)} } ) spawns:addEventListener("touch", whateverFunction) end [import]uid: 84637 topic_id: 23655 reply_id: 95263[/import]

@Danny - Thanks so much! I’m having trouble removing the objects now, though :(… It performs the setLinearVelocity, however it’s not deleting the object upon collision in the bouncerCollision function… I must be misunderstanding something.

[code]
function timerSpawn(event)
local spawns = spawn(
{
image = “images/bouncer.png”,
objTable = spawnTable,
hasBody = true,
radius = 25,
isSensor = true,
group = localGroup,
x = math.random(50, 750),
y = 100
}
)
spawns:setLinearVelocity(0,60)
spawns:addEventListener(“collision”, bouncerCollision)

end

timer.performWithDelay(2000, timerSpawn, 0)

function bouncerCollision( event )
if ( event.phase == “began”) then
player:setLinearVelocity(0,-300)
spawns:removeSelf()
spawns = nil
end
end
[/code] [import]uid: 131400 topic_id: 23655 reply_id: 95287[/import]