Physics problem

local physics = require("physics") -- Start Physics and set the gravity to 0 since the game is in space physics.start() physics.setGravity(0, 0) --physics.setPositionIterations( 16 ) rect = display.newRect(160,300,50,50) physics.addBody(rect, "dynamic") rect.name = "player" function move2(obj) if obj.y \< 250 then obj.y = obj.y +1 else obj:removeSelf() Runtime:removeEventListener("enterFrame",obj) end end function move(obj) if obj.y \> 30 then obj.y = obj.y - 5 else obj:removeSelf() Runtime:removeEventListener("enterFrame",obj) end end function coins(obj) coin = display.newCircle(0,0,5) coin.x , coin.y = obj.x , obj.y coin.name = "coin" physics.addBody(coin, {isSensor = true } ) transition.to(coin, {time =5000, y = 400, onComplete = function () display.remove(coin) coin = nil end; }) end function shoot(e) if e.phase == "ended" then rect1 = display.newRect(0,0,10,10) rect1.x=rect.x rect1.y = rect.y + 20 rect1.name = "bullet" physics.addBody(rect1,{isSensor = true } ) rect1.enterFrame = move Runtime:addEventListener("enterFrame",rect1) end end Runtime:addEventListener("touch", shoot) function onCollision( event ) if(event.object1.name == "bullet" and event.object2.name == "enemy") then display.remove(event.object2) Runtime:removeEventListener("enterFrame",event.object2) display.remove(event.object1) Runtime:removeEventListener("enterFrame",event.object1) coins(event.object2) elseif(event.object1.name == "enemy" and event.object2.name == "bullet") then display.remove(event.object1) Runtime:removeEventListener("enterFrame",event.object1) display.remove(event.object2) Runtime:removeEventListener("enterFrame",event.object2) coins(event.object1) elseif(event.object1.name == "player" and event.object2.name == "coin") then display.remove(event.object2) Runtime:removeEventListener("enterFrame",event.object2) end end function enemySpawn() rect3 = display.newRect(160,0,50,50) rect3.name = "enemy" rect3.numHit = 0 physics.addBody(rect3, {isSensor = true } ) rect3.enterFrame = move2 Runtime:addEventListener("enterFrame",rect3) end enemySpawn() timer.performWithDelay(6000,enemySpawn,0) Runtime:addEventListener("collision",onCollision)

What is the problem of coin physics.addBody ()?

What effect are you seeing (or not seeing)?

It’s hard to tell you what the solution could be if we don’t know what the problem is.

“coin” appears to be global, unless you’ve declared it somewhere else above. This could be one issue, but as @AIanPlantPot says, we don’t know what the reported problem is.

Best regards,

Brent

The coin can’t remove when it collide with rect.

function coins(obj) local objectX, objectY = obj.x, obj.y local function spawnIt() coin = display.newCircle(0,0,5) coin.x , coin.y = objectX , objectY coin.name = "coin" physics.addBody(coin, {isSensor = true } ) transition.to(coin, {time =5000, y = 400, onComplete = function () display.remove(coin) coin = nil end; }) end local spawnCoinTimer = timer.performWithDelay(10, spawnIt) end

Try adding a timer to your coins function…  The problem you are having is that you are adding a new physics object while the collision is happening and that is throwing off the physics engine.

Also… I recommend you get used to utilizing local variables.  Globals are usually a no-no.

What effect are you seeing (or not seeing)?

It’s hard to tell you what the solution could be if we don’t know what the problem is.

“coin” appears to be global, unless you’ve declared it somewhere else above. This could be one issue, but as @AIanPlantPot says, we don’t know what the reported problem is.

Best regards,

Brent

The coin can’t remove when it collide with rect.

function coins(obj) local objectX, objectY = obj.x, obj.y local function spawnIt() coin = display.newCircle(0,0,5) coin.x , coin.y = objectX , objectY coin.name = "coin" physics.addBody(coin, {isSensor = true } ) transition.to(coin, {time =5000, y = 400, onComplete = function () display.remove(coin) coin = nil end; }) end local spawnCoinTimer = timer.performWithDelay(10, spawnIt) end

Try adding a timer to your coins function…  The problem you are having is that you are adding a new physics object while the collision is happening and that is throwing off the physics engine.

Also… I recommend you get used to utilizing local variables.  Globals are usually a no-no.