That have error in ‘contentBounds’(a nil value) when collision
rect = display.newRect(0,0,70,70) rect.x ,rect.y =150,300 local 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 left = obj1.contentBounds.xMin \<= obj2.contentBounds.xMin and obj1.contentBounds.xMax \>= obj2.contentBounds.xMin local right = obj1.contentBounds.xMin \>= obj2.contentBounds.xMin and obj1.contentBounds.xMin \<= obj2.contentBounds.xMax local up = obj1.contentBounds.yMin \<= obj2.contentBounds.yMin and obj1.contentBounds.yMax \>= obj2.contentBounds.yMin local down = obj1.contentBounds.yMin \>= obj2.contentBounds.yMin and obj1.contentBounds.yMin \<= obj2.contentBounds.yMax return (left or right) and (up or down) end function move(obj) if obj .y \> 200 then Runtime:removeEventListener("enterFrame",obj) obj:removeSelf() else obj.y = obj.y + 1 if hasCollided(obj,bullet) then Runtime:removeEventListener("enterFrame",obj) obj:removeSelf() Runtime:removeEventListener("enterFrame",bullet) bullet:removeSelf() end end end function move2(obj) obj.y = obj.y - 4 end function shoot() bullet =display.newRect( 0, 0, 10,10 ) bullet.x = rect.x bullet.y = rect.y - 50 bullet.enterFrame = move2 Runtime:addEventListener("enterFrame",bullet) end function onTouch(e) if e.phase =="began" then shoot() end end function enemy() enemy = display.newRect(150,10,30,30) enemy.enterFrame = move Runtime:addEventListener("enterFrame",enemy) end timer.performWithDelay(2000,enemy,0) Runtime:addEventListener("touch",onTouch)