physics.addBody() error !

Hello ! I dont understand why it doesnt work but im getting this error physics.addBody() cannot be called when the world is locked and in the middle of number crunching, such as during a collision event ! :wacko:

Any help with be greatly appreciated!

ps : forgive my english, I do my best !

local composer = require( “composer” )
local scene = composer.newScene()
local widget = require “widget”
local physics = require “physics”
physics.start()
physics.pause()

 – controle = display.newImage(“Racket.png”,100,100)
local raquette
local balle
local globaleview
local score
local labelScore
local nombreBall
local vie
local labelvie
local nbCible = 30

local sonBump = audio.loadSound(“bump.wav”)
local sonRaquette = audio.loadSound(“raquette.wav”)

function droite()
transition.to( raquette, { time=1500, x = display.screenOriginX + display.actualContentWidth, rotation = 10, onComplete = gauche } )
end

 function gauche()
transition.to( raquette, { time=1500, x = display.screenOriginX, rotation = -10, onComplete = droite } )
end

function Perdvie(pvie)
  vie = vie - pvie
  print("Life : ", vie)
  labelvie.text = "Life: "…tostring(vie)
end

function AjouteScore(pScore)
  score = score + pScore
  print(“Nouveau score”, score)
  – on transforme notre valeur numerique en tostring pour actualiser nautre score
  labelScore.text = “Score:”…tostring(score)
end

function Recible()
  local lig,col ,x,y
  local largeurColonne = (display.actualContentWidth/(5+1))
 
  x = display.screenOriginX + largeurColonne
  y = display.screenOriginY + 100
 
  – si une cible et toucher la Remove et donne des point

     local function onToucheCible(self, event)
    if event.phase == “began” then
      audio.play(sonBump)
      self:removeSelf()
      AjouteScore(25)
      nbCible = nbCible - 1
     print(“Nombre de cible restent”, nbCible)
      end
  end

  for lig = 1,6 do  
    for col = 1, 5 do
      
 
  local cible = display.newCircle(x,y, 8)
      – couleur des cible
      cible:setFillColor(1,math.random(),math.random())
      cible.collision = onToucheCible
      cible:addEventListener(“collision”)
      globaleview:insert(cible)
      x = x + largeurColonne
      
      physics.addBody( cible, “static”, { density = 1, friction = 0.3, bounce = 0.6, radius = 8})
      
    end
    y = y + 50
    x = display.screenOriginX + largeurColonne
  end
end

function CreeNiveau()
  print("Crée  le Niveau 1 ")

  local lig,col ,x,y
  local largeurColonne = (display.actualContentWidth/(5+1))
 
  x = display.screenOriginX + largeurColonne
  y = display.screenOriginY + 100
 
  – si une cible et toucher la Remove et donne des point

    local function onToucheCible(self, event)
    if event.phase == “began” then
      audio.play(sonBump)
      self:removeSelf()
      AjouteScore(25)
      nbCible = nbCible - 1
     print(“Nombre de cible restent”, nbCible)
     if nbCible == 0 then
       Recible()
     end
    end
  end

  for lig = 1,6 do  
    for col = 1, 5 do
      
 
  local cible = display.newCircle(x,y, 8)
      – couleur des cible
      cible:setFillColor(1,math.random(),math.random())
      physics.addBody( cible, “static”, { density = 1, friction = 0.3, bounce = 0.6, radius = 8})
      cible.collision = onToucheCible
      cible:addEventListener(“collision”)
      globaleview:insert(cible)
      x = x + largeurColonne
    end
    y = y + 50
    x = display.screenOriginX + largeurColonne

    
  end

  – Remet le Score a zéro et les ball a 3 defaut game et les vie a 3
  nombreBall = 3
  score = 0
  vie = 3
end

function AjouteBalle(pX)
  if balle ~= nill then
    print(“Deja une balle”)
    return
  end
  – on donne de la physique, density, friction, bounce, radius
  balle = display.newCircle(pX,display.screenOriginY,10)
  physics.addBody(balle, {density = 1.0, friction = 0.3, bounce = 1, radius = 10} )
  globaleview:insert(balle)
 

end

function scene:create( event )
    local sceneGroup = self.view
  globaleview = sceneGroup
 
  – Mon Image que je Centre
  local decor = display.newImageRect(“decor.jpg”, 320,480)
  decor.x = display.contentWidth/2
  decor.y = display.contentHeight/2
 
  – si on touche l ecrant une balle tombe
function onToucheDecor(event)
  AjouteBalle(event.x)
end
decor:addEventListener(“tap”,onToucheDecor)
 
  sceneGroup:insert(decor)

  – on Crée la raquette
  raquette = display.newRoundedRect(0,0,30,5,15)
  raquette.anchorY = 1
 
  – La raquette du Joeur
  controle = display.newRoundedRect(0,0,70,18,2)
  controle.anchorY = 1
  – On la place en bas
  controle.y = display.screenOriginY + display.actualContentHeight
  controle.x = display.screenOriginX
  physics.addBody( controle, “static”, {bounce = 1.3})
 

– Bouger la raquette ( sont nom MoveCircle Juge pas )
local function moveCircle( event )
    controle.x = event.x
end
Runtime:addEventListener( “touch”, moveCircle )

 
 
  – on place la raquette en bas
  raquette.y = display.screenOriginY + display.actualContentHeight - 100
  raquette.x = display.screenOriginX
  physics.addBody( raquette, “static”, {bounce = 1})
 

  – bouge la raquete de droide a gauche
  transition.to  ( raquette, { time = 2000, x = display.screenOriginX + display.actualContentWidth} )
 
  – si la ball touche la raquette fait un bruit de merde
  local function onToucheRaquette(self, event)
      if event.phase == “began” then
      audio.play(sonRaquette)
      AjouteScore(100)
      end
end
 
 controle.collision = onToucheRaquette
 controle:addEventListener(“collision”)
 
 
 
 
  – Mur invisible qui refont rebondir les balle
  – mur Gauche
  local murGauche = display.newRect(display.screenOriginX, display.screenOriginY, 1, display.actualContentHeight)
  murGauche.anchorX = 1
  murGauche.anchorY = 0
  physics.addBody( murGauche,“static”, { bonce = 1} )
 
  – mur Droit
  local murDroite = display.newRect(display.screenOriginX+display.actualContentWidth,display.screenOriginY, 1, display.actualContentHeight)
  murDroite.anchorX = 0
  murDroite.anchorY = 0
  physics.addBody(murDroite,“static”,{ bonce = 1} )
 
    – mur du Haut
  local murHaut = display.newRect(display.contentWidth/2,display.screenOriginY + display.actualContentHeight - 500,display.contentWidth,10)
  murHaut.anchorY = 0
   physics.addBody(murHaut,“static”,{ bonce = 3} )
 
 
 
 
  – La zone du bat
  local zoneFin = display.newRect(display.contentWidth/2,display.screenOriginY + display.actualContentHeight,display.contentWidth,10)
  zoneFin.anchorY = 0
  physics.addBody( zoneFin, “static”, {isSensor = true } )
 
 
  – Si la ball touche la zone de fin elle est Remove et on Perd Une vie
  local function onCollisionZoneFin(self, event)
  print(“touche la zone de fin !”)
  Perdvie(1)
  balle:removeSelf()
  balle = nill
 

 
    – enleve une balle et remais la raquete a 0 pour empécher que deux raquete s affiche qui bug
  nombreBall = nombreBall - 1
  if nombreBall == 0 then
    transition.cancel(raquette)
    dernierScore = score
    composer.gotoScene(“scenemenu”,“fade”,500)
    end
  end
 
  zoneFin.collision = onCollisionZoneFin
  zoneFin:addEventListener(“collision”)
 
 
  – Place l affichage du Score
  labelScore = display.newText(“Score 0”,display.screenOriginX + display.actualContentWidth - 5, 5)
  labelScore.anchorX = 1
  labelScore.anchorY = 0
 
  – Place l’affichage de la vie
  labelvie = display.newText("Life 3 ", display.screenOriginX + display.actualContentWidth - 5, 25)
  labelvie.anchorX = 1
  labelvie.anchorY = 0
 
  sceneGroup:insert( raquette)
  sceneGroup:insert(murGauche)
  sceneGroup:insert(murDroite)
  sceneGroup:insert(zoneFin)
  sceneGroup:insert(labelScore)
  sceneGroup:insert(labelvie)
  sceneGroup:insert(controle)
 
   
 
end

function scene:show( event )
    local sceneGroup = self.view
    local phase = event.phase
 
    if phase == “will” then
    
  CreeNiveau()
  physics.start()
  droite()

if nbCible == 0 then
  cible:destroy()
  Recible()
end
    elseif phase == “did” then
      
      
      
    end    
end

function scene:hide( event )
    local sceneGroup = self.view
    local phase = event.phase
    
    if event.phase == “will” then

    elseif phase == “did” then
        
    end    
end

function scene:destroy( event )
    local sceneGroup = self.view
    

end


– Branchement des événements
scene:addEventListener( “create”, scene )
scene:addEventListener( “show”, scene )
scene:addEventListener( “hide”, scene )
scene:addEventListener( “destroy”, scene )


return scene

Hi @kenny.ng, welcome to the forums!

This code is quite messy, please use the formatting <> tool so it makes helping you easier for others.

Also, this should be a simple fix. Corona should give you a line number pointing the physics.addBody() call. The error is exactly what the simulator is telling you, you can’t call physics.addBody while a collision is happening. To fix this, just call the function in the next frame:

timer.performWithDelay(1, function() physics.addBody(object, bodyType) end, 1)

Hi @kenny.ng, welcome to the forums!

This code is quite messy, please use the formatting <> tool so it makes helping you easier for others.

Also, this should be a simple fix. Corona should give you a line number pointing the physics.addBody() call. The error is exactly what the simulator is telling you, you can’t call physics.addBody while a collision is happening. To fix this, just call the function in the next frame:

timer.performWithDelay(1, function() physics.addBody(object, bodyType) end, 1)