Assertion failed error when dynamically creating a new physics body

I have the following code in which when the player physics body collides with the sensor body, a new physics body is spawned.
The new body is created but I get a
"Assertion failed. Expression:isLocked == false() "
error and Corona crashes…

[lua]physics = require “physics”
physics.start()
physics.setGravity(0,10)
ground = display.newRect(100,400,500,51);
physics.addBody(ground,“static”)

player = display.newRect(100,340,100,100); player.x = 100; player.y =340;
physics.addBody(player)
sensor = display.newRect(300,0,20,800)
sensor.name = “sensor”
physics.addBody(sensor,“static”,{isSensor = true})

local onEnterFrame = function(event)
player.x = player.x + 5
end

Runtime:addEventListener(“enterFrame”,onEnterFrame)

local onCollide = function(self,event)
if(event.other.name == “sensor”) then
dynamic = display.newRect(700,340,100,100)
physics.addBody(dynamic,“static”)
end
end
player.collision = onCollide
player:addEventListener(“collision”,player)[/lua]

If I click on ignore… corona continues… What must I do to avoid this error? [import]uid: 64174 topic_id: 12544 reply_id: 312544[/import]

In the code below the player will detect the collision with sensor and change sensor’s color. Does you app require you to use .isSensor?

[lua]physics = require “physics”
physics.start()
physics.setGravity(0,10)

ground = display.newRect(100,400,500,51);
physics.addBody(ground,“static”)

player = display.newRect(100,340,100,100); player.x = 100; player.y =340;
player.name = “player”
physics.addBody(player, “dynamic”)

sensor = display.newRect(300,0,20,800)
sensor.name = “sensor”
physics.addBody(sensor,“static”,{})

local onEnterFrame = function(event)
player.x = player.x + 5
end

Runtime:addEventListener(“enterFrame”,onEnterFrame)

function onCollision2( event )
if ( event.phase == “began” ) then
if(event.object1.name == “player” and event.object2.name == “sensor”) then
sensor:setFillColor(155,63,234);
end
end
end

Runtime:addEventListener( “collision”, onCollision2 )
[import]uid: 17138 topic_id: 12544 reply_id: 45824[/import]

I dont think you got my question! I need to create a completely new
physics body when the player comes in contact with the sensor… (not change the sensor colour???).

And yes I need isSensor = true… [import]uid: 64174 topic_id: 12544 reply_id: 45825[/import]

You need to use a delay.

for instance

–On collision
local function delay()
local dynamic = display.newRect(700,340,100,100)
physics.addBody(dynamic,“static”)
end
–Add physics body after a tiny delay (not noticeable)
timer.performWithDelay(100, delay) [import]uid: 6981 topic_id: 12544 reply_id: 45826[/import]

Thanks a lot @infuseddreams! It works perfectly!

Any idea why I need to include a delay?
[import]uid: 64174 topic_id: 12544 reply_id: 45829[/import]

I understood you don’t want to change the color, I thought you would take that part out and implement what you wanted. Anyways, here is the code for when player collides with the wall a ball spawns. Im not sure what you want the .isSensor to do, so you will have to implement that in.
[lua]physics = require “physics”
physics.start()
physics.setGravity(0,10)

_W = display.contentWidth
_H = display.contentHeight

ground = display.newRect(100,400,500,51);
physics.addBody(ground,“static”)

player = display.newRect(100,340,100,100); player.x = 100; player.y =340;
player.name = “player”
physics.addBody(player, “dynamic”)

sensor = display.newRect(300,0,20,800)
sensor.name = “sensor”
physics.addBody(sensor,“static”,{})

local onEnterFrame = function(event)
player.x = player.x + 5
end

Runtime:addEventListener(“enterFrame”,onEnterFrame)

function onCollision2( event )
if ( event.phase == “began” ) then
if(event.object1.name == “player” and event.object2.name == “sensor”) then
Runtime:removeEventListener(“enterFrame”, onEnterFrame);
local function spawn()
ball = display.newCircle(0, 0, 9);
ball.y = _H * 0.5 - 5;
ball.x = _W * 0.5 + 30;
physics.addBody(ball, “dynamic”,{radius = 9, mass =10, density = 1});
end
timer.performWithDelay(100, spawn, 1);
end
end
end

Runtime:addEventListener( “collision”, onCollision2 )

–By the way, .isSensor is a body property, so if I wanted to add it to player I would do this
–player.isSensor = true; [import]uid: 17138 topic_id: 12544 reply_id: 45830[/import]

@satheeshrulzz : Your welcome. Yeah, you have to be mindful of doing things in the exact same time-step as a physics event. Things like changing variables, applying velocity etc is fine. But adding bodies or such requires a delay before execution. It’s a box2d thing. [import]uid: 6981 topic_id: 12544 reply_id: 45833[/import]

Ok! Thanks a lot for the help!

and Thanks @Khaodik for the code!

:slight_smile: [import]uid: 64174 topic_id: 12544 reply_id: 45834[/import]