I have a error attempt to perform arithmetic on field ‘x’ (a nil value), when I press the button after the bullet collided with the enemy
player = display.newRect(0,0,50,50) player.x =110 player.y = 110 b1 = display.newCircle(0,0,30) b1.x ,b1.y = 210 , 110 function hasCollided( obj1, obj2 ) if ( obj1 == nil ) then --make sure the first object exists return false end if ( obj2 == nil ) then --make sure the other object exists return false end local dx = obj1.x - obj2.x local dy = obj1.y - obj2.y local distance = math.sqrt( dx\*dx + dy\*dy ) local objectSize = (obj2.contentWidth/3) + (obj1.contentWidth/3) if ( distance\< objectSize+3) then return true end return false end local function moveBullet(obj) if obj.y \< 500 then obj.y = obj.y + 8 if hasCollided(obj,E1) then E1:removeSelf() Runtime:removeEventListener("enterFrame",E1) obj:removeSelf() Runtime:removeEventListener("enterFrame",obj) end else obj:removeSelf() Runtime:removeEventListener("enterFrame",obj) obj = nil end end function mBullet(e) if e.phase == "ended" then bullet = display.newCircle(0,0,5) bullet.x =player.x bullet.y = player.y+ 40 bullet.enterFrame = moveBullet Runtime:addEventListener("enterFrame",bullet) end end function move(obj) if obj.y \>-30 then obj.y = obj.y - 2 else obj:removeSelf() Runtime:removeEventListener("enterFrame",obj) obj = nil end end function enemy() E1 = display.newRect(0,0,40,20) E1.y =400 E1.x = 110 E1.enterFrame = move Runtime:addEventListener("enterFrame",E1) end b1:addEventListener("touch",mBullet) timer.performWithDelay(2500,enemy,0)