Just wondering is it possible for physics to be added to objects onscreen and removed to objects off screen? I’m making a pretty big sandbox game that requires free roaming and for objects to be spawned off screen to the player
I have followed danny’s tutorial for spawning objects - the right way!
and my code goes somewhat like this.
[lua]
local function spawnPeeps(params)
xRandom = math.random(1,10000)
yRandom = math.random(1,10000)
local peeps = display.newCircle( xRandom, yRandom, 15)
–Set the objects table to a table passed in by parameters
peeps.objTable = params.objTable
–Automatically set the table index to be inserted into the next available table index
peeps.index = #peeps.objTable + 1
– local peepsNumber = display.newText(peeps.index, xRandom, yRandom, nil, 12)
– peepsNumber:setTextColor(0,0,0)
print(peeps.index, #peeps.objTable )
–Give the object a custom name
peeps.myName = "peeps : " … peeps.index
–If the object should have a body create it, else dont.
if params.hasBody then
–Allow physics parameters to be passed by parameters:
peeps.density = params.density or 0
peeps.friction = params.friction or 0
peeps.bounce = params.bounce or 0
peeps.isSensor = params.isSensor or false
peeps.bodyType = params.bodyType or “dynamic”
physics.addBody(peeps, peeps.bodyType, {density = peeps.density, friction = peeps.friction, bounce = peeps.bounce, isSensor = peeps.isSensor})
end
– checking whether it’s a peep
peeps.isPeep = true
–check if alive
peeps.isAlive = true
–checking collision with phys trig
peeps.isActive = true
–The objects group
peeps.group = params.group or nil
–If the function call has a parameter named group then insert it into the specified group
peeps.group:insert(peeps)
–Insert the object into the table at the specified index
peeps.objTable[peeps.index] = peeps
return peeps
end
–Create a table to hold our spawns
local spawnTable = {}
params = {
objTable = spawnTable,
hasBody=true,
friction = 0.4,
bounce = 0.4,
bodyType = “dynamic”,
group = group,
}
–Spawn two objects
for i = 1, 2000 do
local spawns = spawnPeeps(params)
end
[/lua] [import]uid: 87730 topic_id: 36384 reply_id: 336384[/import]