Adding physics body problem

Hi, Im doing something like this:

I press the button and spawn an object1… On the scene are others objects and if spawned object1 collide with other object2 then:
Im spawed new object3 on Object2.x and Object2.y
Object2 will remove self…

Works without problems… but:

I need to add physics body to Object3 … If I try it in collision function, simulator will crash with bus error :confused:

So I put code for adding body out of the collision function and get:

Runtime error  
 /Users/HP6/Desktop/HB 17.9.2011/level2-1.lua:422: ERROR: table expected. If this is a function call, you might have used '.' instead of ':'  

But in line 422 I have ‘.’ instead of ‘:’ …

physics.addBody(object3, {density=4, friction=3, bounce=0})  

I found this:
http://developer.anscamobile.com/forum/2011/05/26/bus-error

But if I do it with timer simulator still crashing when adding physics body…

What can be wrong?
Thanks.

P.S. : Sorry for my English, Hope you can understand :wink: [import]uid: 59968 topic_id: 15235 reply_id: 315235[/import]

need to see more code to see whats happening. but just a quick thought ? did you do physics.start() ? [import]uid: 71210 topic_id: 15235 reply_id: 56271[/import]

Yes I did…
With pressing button Im adding more physics bodies to others objects, and they works normally… only this one which is created on collision doesn’t work…
I dont understand why … :frowning: [import]uid: 59968 topic_id: 15235 reply_id: 56273[/import]

Here is quite more code:

[code]
physics.start()

if object1 ~= nil then
object1:addEventListener(“collision”,object1)
function object1:collision (event)
if event.other.myName == “object2” then
object1:removeEventListener(“collision”,object1)
object3 = display.newImageRect(“images/object3.png”, 43, 43);
object3.x = object1.x; object3.y = object1.y;
object3:setReferencePoint(display.c)
object3.myName = “object3”;
localGroup:insert(object3);
physics.addBody(object3, {density=4, friction=3, bounce=0})
event.target:removeSelf()
event.other:removeSelf()
object1 = nil
end
end
end
[/code] [import]uid: 59968 topic_id: 15235 reply_id: 56274[/import]

am getting a different error
ERROR: physics.addBody() cannot be called when the world is locked and in the middle of number crunching, such as during a collision event. [import]uid: 71210 topic_id: 15235 reply_id: 56285[/import]

you can delay the spawning for few milliseconds to avoid this error
see the below code
[lua]local physics = require(“physics”)
physics.start()
physics.setGravity(0,9.8)
local localGroup = display.newGroup()

local object1 = display.newCircle(100,100,20)
physics.addBody(object1,“dynamic”)

local object2 = display.newRect(50,250,60,20)
physics.addBody(object2,“static”)
object2.myName = “object2”

local x,y
local function spawnObject()
local object3 = display.newCircle(100,100,30)
object3.x = x; object3.y = y;
object3:setReferencePoint(display.c)
object3.myName = “object3”;
localGroup:insert(object3);
physics.addBody(object3,{density=4, friction=3, bounce=0})
end

local function object1collision (event)
if event.phase ==“ended” then
if event.other.myName == “object2” then
x = object1.x ; y =object1.y
timer.performWithDelay(10,spawnObject,1)
event.target:removeSelf()
event.other:removeSelf()
object1 = nil
end
end
end
object1:addEventListener(“collision”,object1collision)[/lua] [import]uid: 71210 topic_id: 15235 reply_id: 56286[/import]

Thanks! It solved my problem :), but now Im trying this:
in spawnObject function Im adding collision listener to object3

local x,y  
local function spawnObject()  
 local object3 = display.newCircle(100,100,30)  
 object3.x = x; object3.y = y;  
 object3:setReferencePoint(display.c)  
 object3:addEventListener("collision",object3)  
 object3.myName = "object3";  
 localGroup:insert(object3);  
 physics.addBody(object3,{density=1.3, friction=3, bounce=0})  
end  

So, then i create object4 which collide with object3 add some rules to destroy object3 “oncolllide” and nothing happens…

I dont understand why… maybe I should use timer again to add physics.addBody(object3,{density=1.3, friction=3, bounce=0}) ?
Thanks again :wink: [import]uid: 59968 topic_id: 15235 reply_id: 56403[/import]

Bump :slight_smile: [import]uid: 59968 topic_id: 15235 reply_id: 56693[/import]

Sorry I din’t notice your last post.
hope this will help
[lua]local physics = require(“physics”)
physics.start()
physics.setGravity(0,9.8)
local localGroup = display.newGroup()

local object1 = display.newCircle(100,100,20)
physics.addBody(object1,“dynamic”)

local object2 = display.newRect(50,250,60,20)
physics.addBody(object2,“static”)
object2.myName = “object2”

local object4 = display.newRect(50,400,60,20)
physics.addBody(object4,“static”)
object4.myName = “object4”

local function object3Collision(event)
event.target:removeSelf()
end

local x,y
local function spawnObject()
local object3 = display.newCircle(100,100,30)
object3.x = x; object3.y = y;
object3:setReferencePoint(display.c)
object3.myName = “object3”;
object3:addEventListener(“collision”,object3Collision)
localGroup:insert(object3);
physics.addBody(object3,{density=4, friction=3, bounce=0})
end

local function object1collision (event)
if event.phase ==“ended” then
if event.other.myName == “object2” then
x = object1.x ; y =object1.y
timer.performWithDelay(10,spawnObject,1)
event.target:removeSelf()
event.other:removeSelf()
object1 = nil
end
end
end
object1:addEventListener(“collision”,object1collision)[/lua] [import]uid: 71210 topic_id: 15235 reply_id: 56790[/import]

Thank you for the idea whit the delay … i was stuck at his error [import]uid: 169726 topic_id: 15235 reply_id: 122615[/import]

Thank you for the idea whit the delay … i was stuck at his error [import]uid: 169726 topic_id: 15235 reply_id: 122615[/import]