Hi guys! I’ve been stuck on this issue for a few days now.
What I’m trying to do are 2 things:
- remove the cannonball on collision.
- 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]
