Problem setting collisions

Errrmmm… 

Hello ppl, I’ve been having problems setting up collisions in my game

Sorry for the messy code(im a 13 year old kid who is unorganized…lol)

------------------------------------------------------ ------------------------------------------------------ -----------------HELICOPTER BLAST--------------------- ------------------------------------------------------ ------------------------------------------------------ --EVERYTHING IS MESSY HERE... local storyboard = require ("storyboard") local scene = storyboard.newScene() local addrotation local Group = display.newGroup() local physics= require ("physics") local ui = require ("ui") local canfire = true local ammocap = 3 ispause = false local canfire = true local ammocap0 = false thislevel = "level1" --physics stuff physics.start() physics.setGravity(0,10) physics.setDrawMode("hybrid") --cannon itself local cannon = display.newImage("cannon.png") cannon.rotation = -35 cannon.xScale = 0.08 cannon.yScale = 0.08 cannon.x = 70 cannon.y = 240 cannon:toFront() -- the ground local ground = display.newImage("Road.png") ground.x = 240 ground.y = 310 ground.xScale = 1.5 physics.addBody(ground, "static",{bounce = 0, shape = {-240,-25, 500,-25, 500,25, -240,25}}) --ballon(will be replaced with a helicopter :D) --FOR THE PEOPLE WHO ARE READING THIS --I SPELLED BALLOON WRONG CUZ IDK WHY IM JUST --USED TO SPELLING THIS WAY... --DONT JUDGE ME --LOOOOOL~ local spawnballon1 = function() ballon = display.newImage("ballon.png") physics.addBody(ballon, "kinematic", {bounce = 0, radius = 18, density = 0}) ballon.x = 300 ballon.y = 100 ballon.xScale = 0.2 ballon.yScale = 0.2 ballon.name = "ballon" ballonpop = false end timer.performWithDelay(1, spawnballon1) local spawnballon2 = function() --balloon 2 test ballon2 = display.newImage("ballon.png") physics.addBody(ballon2, "kinematic", {bounce = 0, radius = 18, density = 0}) ballon2.x = 300 ballon2.y = 160 ballon2.xScale = 0.2 ballon2.yScale = 0.2 ballon2.name = "ballon2" ballon2pop = false end timer.performWithDelay(1, spawnballon2) --cannon rotation calculation cannontouch = function(event) --print (event.x, event.y) if event.x \< 200 and event.x \> 20 and event.y \> 180 and event.y \< 300 then if event.yStart \> event.y then --print ("up") uprottemp = event.yStart - event.y uprot1 =uprottemp /50 --modify here to change rotation sensitivity rot = uprot1 \* 3 cannon.rotation = cannon.rotation - rot end if event.yStart \< event.y then --print ("down") downrottemp = event.y - event.yStart downrot1 =downrottemp / 50 --modify here to change rotation sensitivity rot = downrot1 \* -3 cannon.rotation = cannon.rotation - rot end end --ROTATION LIMITS --print(cannon.rotation) if cannon.rotation \< -85 then cannon.rotation = -85 end if cannon.rotation \> -5 then cannon.rotation = -5 end --print(cannon.rotation) rotation = (cannon.rotation \* -1) angle = rotation / 180 radian = angle / math.pi --force calculations --DO NOT MODIFY THIS SHIT...unless needed ammox = 60 + 1500 \* math.cos(9.75\*radian) ammoy = 230 - 2700 \* math.sin(6.75\*radian) --where the ammo will transition.to TO then fire ammoxpos = 60 + 70 \* math.cos(9.75\*radian) ammoypos = 230 - 70 \* math.sin(6.75\*radian) --print(rotation) end Runtime:addEventListener("touch" , cannontouch) --giveammo local giveammo = function() ammo = display.newImage("cannonball.png") ammo:toBack() ammo.isVisible = false candelete = true ammo.name = "ammo" ammo.xScale = 0.02 ammo.yScale = 0.02 ammo.x = 70 ammo.y = 240 ammo.collision = ammocollision Group:insert(ammo) local ammocollision = function(event) if event.phase == "began" then if event.object1.name == "ballon2" then print("ballon2 is popped") ballon2pop = true ballon2:removeSelf() ammo:removeSelf() ammo = nil end if event.object1.name == "ballon" then print("ballon is popped") ballon:removeSelf() ballonpop = true ammo:removeSelf() ammo = nil end canfire = true candelete = false end end Runtime:addEventListener("collision", ammocollision) end --permissions local fireperm = function(event) if event.phase == "press" and ispause == false and canfire == true then canfire = false if ammocap \>= 1 then timer.performWithDelay(1, giveammo) local fireammo = function() physics.addBody(ammo, "dynamic", {density =7, radius = 10, friction = 0.2, bounce = 0.2}) ammocap = ammocap - 1 print ("FIRE!") transition.to(ammo,{x = ammoxpos, y = ammoypos, time = 100}) ammo:applyForce(ammox,ammoy) end timer.performWithDelay(600, function() ammo.isVisible = true end) timer.performWithDelay(200, function() Runtime:removeEventListener("touch" , cannontouch) end) timer.performWithDelay(1500, function() Runtime:addEventListener("touch" , cannontouch) end) --modify here to change ammo visible time timer.performWithDelay(500, fireammo) end print("cannonballs left = "..ammocap) end if ammocap == 0 and candelete == false then local noammo = display.newText("NO AMMO!",0,0,native.systemFont, 40) noammo.x = 240 noammo.y = 160 timer.performWithDelay(500, function() noammo:removeSelf() end) end end --pause to gameplay transition in pause.lua setgroupalpha1 = function() Group.alpha = 1 end setgroupalpha09 = function() Group.alpha = 0.9 end local pausesim = function() storyboard.gotoScene("pause") Group.alpha = 0.7 end local pausesimperm = function(event) if ispause == false and event.phase == "press" then timer.performWithDelay(1,pausesim) end end --restart game local restart = function(event) if event.phase == "press" and ispause == false then if ballonpop then timer.performWithDelay(1, spawnballon1) end if ballon2pop then timer.performWithDelay(1, spawnballon2) end cannon.rotation = -35 ammocap = 3 canfire = true ammo = nil if candelete == true then ammo:removeSelf() end end end --FIRE BWATTON!!!! local firebtn = ui.newButton{ defaultSrc = "button.png", defaultX = 70, defaultY = 70, overSrc = "button.png", overX = 60, overY =60, onEvent = fireperm, } firebtn.x = 20 firebtn.y = 200 local pausebtn = ui.newButton{ defaultSrc = "button.png", defaultX = 30, defaultY = 30, overSrc = "button.png", overX = 20, overY = 20, onEvent = pausesimperm, } pausebtn.x = 10 pausebtn.y = 10 local restartbtn = ui.newButton{ defaultSrc = "button.png", defaultX = 30, defaultY = 30, overSrc = "button.png", overX = 20, overY = 20, onEvent = restart, } restartbtn.x = 35 restartbtn.y = 10 --INSERTS Group:insert(cannon) Group:insert(firebtn) Group:insert(pausebtn) Group:insert(ground) Group:insert(restartbtn) --storyboard shit function scene:createScene(event) end function scene:enterScene() end function scene:exitScene() end function scene:destroyScene(event) end scene:addEventListener("createScene", scene) scene:addEventListener("enterScene", scene) scene:addEventListener("exitScene", scene) scene:addEventListener("destroyScene", scene) return scene

bit long, but any help would be appreciated…

Error msg:

attempt to index local “ammo” a nil value

How it happened…

Whenever i shoot the second ballon after i killed the first one, its tells me that the ballon is nil, but I dont know why, If i press the fire button, it creates a new ammo, shooting it out. And after the collision takes place, i return the ammo value as nil and remove it.  By the way, collision is fine on the first shot, but not on the second.

PLEASE HELP…

hi @gauss.roy

You are trying to remove a nil object.

change all your

  ammo:removeSelf()

  

to

 display.remove(ammo)

what this does is it will check ammo is not nil before then attempt to delete.

Happy coding!

burhan

ERRR…

now i realized i dont need so many functions…

LOL~

thanks :slight_smile:

You’re welcome. :slight_smile:

Yes you have some unused functions.

I also noticed you use ui library for your buttons which is old and unsupported.

I suggest you use widget.newButton instead in the future. 

Good luck!

burhan

hi @gauss.roy

You are trying to remove a nil object.

change all your

  ammo:removeSelf()

  

to

 display.remove(ammo)

what this does is it will check ammo is not nil before then attempt to delete.

Happy coding!

burhan

ERRR…

now i realized i dont need so many functions…

LOL~

thanks :slight_smile:

You’re welcome. :slight_smile:

Yes you have some unused functions.

I also noticed you use ui library for your buttons which is old and unsupported.

I suggest you use widget.newButton instead in the future. 

Good luck!

burhan