hi everybody,
in this game when a player lands on the border dies. Each time, it triggers a next level if the number of players killed the equal level.
The problem is that when i have fast collisions i have this bug :
physcis.removeBody() given a display object that is not a physic object
…why? Can you help me ?
-------------------------------------------------------------------------------- -- require -------------------------------------------------------------------------------- local physics = require("physics") physics.start() physics.setScale(15) physics.setGravity(0,3) -------------------------------------------------------------------------------- --variables -------------------------------------------------------------------------------- local border local player={} local level = 1 local cnt=0 local flagStart=true -------------------------------------------------------------------------------- -- load Elements -------------------------------------------------------------------------------- local function gameInit() border=display.newRect(100,400,100000,10) physics.addBody(border,"static") for i=1, 9 do player[i]=display.newCircle(200+i\*5,100,5) player[i].name="player" player[i].isDead=false player[i].isVisible=false player[i].kill = function() function kill() if not ( physics.removeBody( player[i] ) ) then print( "Could not remove physics body" ) else print("ok") player[i].isDead=true player[i].isVisible=false end end timer.performWithDelay(5,kill) end end end -------------------------------------------------------------------------------- -- appears Elements -------------------------------------------------------------------------------- local function startLevel(w) --put the player at the top to fall again on the border for i=1, w do print("startLevel") player[i].y=i-100 player[i].isDead=false player[i].isVisible=true physics.addBody(player[i]) flagStart=true end end ---------------------------------------------------------------------- gameInit() startLevel(1) local function onCollision(event) if event.phase == "began" then if event.other.name == "player" then print("collision began with player") event.other.kill() end elseif event.phase =="ended" then if event.other.name == "player" then print("collision ended with player") cnt = cnt +1 --count the number of player dead if cnt == level and flagStart then flagStart=false --to prevent multiple startlevel() cnt=0 --to reset the count because we start a new level level=level+1 if level \> #player then level =1 --to loop end timer.performWithDelay(10,function() startLevel(level) end) end end end end border:addEventListener( "collision",onCollision )