I’m having trouble removing my “bouncer” object when my player collides with it. It performs the setLinearVelocity, however it does not remove the “bouncer”. Attached is all of the code associated with “bouncer” spawning and collision. Note the ground removes as intended upon initial contact with the “bouncer” as well.
--Bouncer Spawning----------------------------------------------------
--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.radius = params.radius or 0
object.isSensor = params.isSensor or false
object.bodyType = params.bodyType or "kinematic"
physics.addBody(object, object.bodyType, {velocity, density = object.density, friction = object.friction, bounce = object.bounce, radius = object.radius,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
--Location the Draw the 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 the bouncers!
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 = bounceSpawnLocation.y
}
)
spawns:setLinearVelocity(0,60)
spawns:addEventListener("collision", bouncerCollision)
end
timer.performWithDelay(1200, timerSpawn, 0)
--Bouncer Collision
function bouncerCollision( event )
if ( event.phase == "began") then
if ground == nil then
player:setLinearVelocity(0,-300)
elseif ground then
player:setLinearVelocity(0,-300)
ground:removeSelf()
ground = nil
end
end
end
Also – Is there a way to make it so that when the player exits the view area of the bottom of the screen, the application quits? [import]uid: 131400 topic_id: 23990 reply_id: 323990[/import]
[import]uid: 69826 topic_id: 23990 reply_id: 96646[/import]
[import]uid: 131400 topic_id: 23990 reply_id: 96650[/import]