Trouble removing objects...

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]

In the code above i don’t actually see any code that is set to remove the “bouncer” object. Unless I’m being blind (and i often am lol) the only objects being removed at all in the code is the ground. Have you pasted in all the code related to removing the “bouncer” object?

Also in regards to your last question, you can use: os:exit() to force the app to exit at any point. It seems a little drastic to shut down the app if the player moves off the screen, but it could be quite entertaining :smiley: [import]uid: 69826 topic_id: 23990 reply_id: 96646[/import]

Actually, I removed the code related to removing the bouncer object. It was previous spawns.removeSelf(), which didn’t work.
Also – I knew about os:exit(), I was fishing for some code that would allow me to call it when my player left the bottom of the view area, if that’s possible.
Thanks a ton for the quick response :slight_smile: [import]uid: 131400 topic_id: 23990 reply_id: 96650[/import]

Ah i see…

Well you could try something like this in the collision listener…

--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  
  
 --You'd replace the .name bit with what you actually call your bouncers though..  
 if event.object1 ~= nil and event.object1.name == "bouncer" then  
 display.remove(event.object1); event.object1 = nil  
  
 elseif event.object2 ~= nil and event.object2.name == "bouncer" then  
 display.remove(event.object2); event.object2 = nil  
 end  
 end  
end  

Im not sure if that will actually work, but its worth giving it a shot lol. Im using event.object’s in my current game to listen to collisions and it seems to work quite well.
Oh ok then, i see what you mean now. Well you would have to keep track of your players x and y position. I would recommend…

local \_H = display.contentHeight  
  
local function gameLoop( event )  
 --You would change the below to whatever you call your player..  
 -- All this does is check to see if the player is beyond the height allowed and then if they are, it closes the app.  
 if player ~= nil and player.y \> \_H then  
 os:exit()  
 end  
end   
Runtime:addEventListener( "enterFrame", gameLoop )  

All the code i’ve written in both the chunks above i haven’t tested at all, so it probably isn’t plug and play code, but you get the general idea of what I’m trying to do :smiley: [import]uid: 69826 topic_id: 23990 reply_id: 96654[/import]

The os.exit() code works great :slight_smile:

The bouncers still aren’t removing, however…

I get an "attempt to index object1 (a nil value) error… hmm…
I’m open to completely redoing the bouncer collision code at this point… I’m wondering if there’s not a better way in general lol [import]uid: 131400 topic_id: 23990 reply_id: 96656[/import]

Good good :slight_smile:

Hmm ok then… it may be because your calling your collisions as: spawns:addEventListener(“collision”, bouncerCollision)…

Can you try it as Runtime:addEventListener( “collision”, bouncerCollision)? Obviously you should only call that once, so place it further down your code instead of in the spawn function.

Im not sure if that would make a difference, but its worth a shot.

If that doesn’t work… i’ll have a think and see if theres a better way that would actually work :smiley: [import]uid: 69826 topic_id: 23990 reply_id: 96658[/import]

That gives me an error with Director: Failed to execute new(params)function.

Assertion Failed!

Doh… I’m thinking I tried that already last night :stuck_out_tongue:
oops – I moved it farther down and it seems to register now, but still no go. Here’s the new code.

[code]
–Bouncer Collision
function bouncerCollision( event )
if ( event.phase == “began” and event.object1 ~= ground) then
if ground == nil then
player:setLinearVelocity(0,-300)
elseif ground then
player:setLinearVelocity(0,-300)
ground:removeSelf()
ground = nil
end

if event.object1.name == “bouncer” then
object1.removeSelf()
event.object1 = nil
end

end
end

Runtime:addEventListener(“collision”, bouncerCollision)
[/code] [import]uid: 131400 topic_id: 23990 reply_id: 96664[/import]

I had the event.object code all wrong. It works great now. Thanks a ton! Couldn’t have done it without you! :slight_smile: [import]uid: 131400 topic_id: 23990 reply_id: 96665[/import]

Phew, i was going through my code trying to figure out why mine works and yours didn’t lol.

Awesome, glad its working for you now! No problem. [import]uid: 69826 topic_id: 23990 reply_id: 96668[/import]