remove object on collision

Hi guys! I’ve been stuck on this issue for a few days now.
What I’m trying to do are 2 things:

  1. remove the cannonball on collision.
  2. OR remove the cannonball when it passes x coordinate 300.
    when the cannon hit the dragon, I’m getting an error saying “attempt to compare number with nil”
    I’m new so my codes might be wrong. Thank you for any helps in advance.

[lua]local steelBalls = {}

–**************************************************************************************************
–================ Collision ===========================================================
local onDragonCollision = function ( self, event )

if event.phase == “began” then

if event.other.objectName == “grass” then

self.isVisible = false
self.isBodyActive = false

self:removeSelf()

elseif event.other.objectName == “greendragonInstance” then

self.isVisible = false
self.isBodyActive = false

for b = #grndragons, 1, -1 do

local removedragon = function()
event.other:removeSelf()
table.remove(grndragons, b)
end

local removedragonTimer = timer.performWithDelay(1, removedragon, 1)
end

self:removeSelf()
end

end
end

–**************************************************************************************************
–===================== Cannon Balls ===============================

local cannonSteelBall = function (event)
steelball = display.newImage ( “steelball.png”)

physics.addBody(steelball, “dynamic”, { density = 0.55, friction = 0.1, radius = 12.5})
steelball.objectName = “steelball”
steelball.x = cannonball.x
steelball.y = cannonball.y
steelball.rotation = cannonball.rotation
steelball.isVisible = true
steelball.isBullet = true
steelball.isBodyActive = true

levelGroup:insert(steelball)
gameGroup:insert(levelGroup)

local xForce = 210 - (((180 - cannongun.rotation)/90) * 210)
local yForce = 210 * ((cannongun.rotation - 180)/180)

steelball:applyForce( xForce, yForce, steelball.x, steelball.y)

steelball.collision = onDragonCollision
steelball:addEventListener(“collision”, steelball)

steelBalls[#steelBalls + 1] = steelball

end

–**************************************************************************************************
–=============== Remove Out of Bound Cannon Balls =====================
local removeSteelBall = function ()
local oldball

for k=#steelBalls, 1, -1 do

oldball = steelBalls[k]

if(oldball.x > 300 ) then – line 399
oldball:removeSelf()
table.remove(steelBalls, k)
end
end
end
Runtime:addEventListener(“enterFrame”, removeSteelBall)

–**************************************************************************************************
–============== fire cannon ball Touch Function goes below here --================================

local fireCannon = function ( event )

if event.phase == “ended” then
local cannonSteelBallTimer = timer.performWithDelay(1, cannonSteelBall, 1)

end
end [import]uid: 12455 topic_id: 5800 reply_id: 305800[/import]

is it possible both have happened so one fails?

sounds like oldball has been removed so oldball.x doesn’t exist

rather than removing in 2 places, i would set a flag in the collision to say “this ball should be removed” and then in my enterframe loop over all active balls and remove the ones with that flag

eg [lua]if ((oldball.x < 300) or (oldball.removeMe == true))[/lua]

[import]uid: 6645 topic_id: 5800 reply_id: 19906[/import]

Hello jmp909,
thank you for getting the time to read my request.

I have completed my first game game and really appreciates if would have the time to take a look on it and give you your feedback.

This is my first game ever, i haven’t done any game before, and it was really my first experiance and would like to give me your feedback. i have learned Corona and LUA in 4 weeks and just came with this game. I know you may not be satisfied with the code but really i have done my best and would like to have your expertise and suggestion in this regard.

Actually, i have 2 problems; the first problem relates to the craches of the app as i have noticed that the app consumed a lot of texture memory on each stage and therefore, i’m sure there is a serious problem.

second, the app becomes slow on my device at early stages.

One more this, i really would like to know if my code meets at least the minimum requirements form Development Prospective in Corona. In other words, does the overall code shows that i’m working in the correct path?

Please if you have the time just reply back to me to zip the code and email to to you.

Thanks again [import]uid: 11038 topic_id: 5800 reply_id: 19924[/import]

iphone_2010… you know I don’t work for Ansca right? I’ve been learning Lua/Corona for about as long as you have :stuck_out_tongue:

[import]uid: 6645 topic_id: 5800 reply_id: 20002[/import]

JMP909, thank you for your reply. I’m not sure how to do that or where to put it. Maybe you can provide me a little example. Thank you for your help.
[import]uid: 12455 topic_id: 5800 reply_id: 20027[/import]

What’s the proper way to:

  1. spawn multiple objects using the same “object code” then

  2. move the objects around with an Runtime:EventListener(“enterFrame”)

  3. remove any of the spawned objects that passes through a certain coordinate without affecting the other spawned objects. Finally,

  4. remove any objects that come in collision with each other without
    affecting the other moving or spawned objects.

If any has any sample codes, I would really appreciate it.
Edited: I found the solution using transition.to on another post. Thank you.

[import]uid: 12455 topic_id: 5800 reply_id: 20096[/import]