Corona Simulator crash when add physics body

I’m working on posting my game to Corona and came a cross a weird bug/crash that I cannot figure out.

I have a runtime collision listener that calls a function that does some logic and in turn might call a third function that creates a new display element and attaches a physics body to it.
The problem/crash occurs when this third function is actually called. I have narrowed down the issue to the line that actually attaches the physics body to the display object.

The weird things is `i directly call this function it behaves normally and everything is ok. It is only when it is called via the listener that simulator crashes.

Anyone has any idea whats going on? [import]uid: 6942 topic_id: 10338 reply_id: 310338[/import]

reading through the docs, it seems that maybe I have to use a performWithDelay. But even so, the simulator still crashes. [import]uid: 6942 topic_id: 10338 reply_id: 37733[/import]

hmm. I’m using the following timer to call the function but it seems that it is being evaluated immediately regardless of the delay I put in:

[lua]timer.performWithDelay(100, action(e) , 1)[/lua] [import]uid: 6942 topic_id: 10338 reply_id: 37735[/import]

Hey there,

A delay of 100 is only 1/10th of a second so it would likely appear immediate to you - 1000 is one second, so keep that in mind.

Next, when are you adding your physics body? There are times that the simulator will crash if the body is being added incorrectly.

Peach :slight_smile: [import]uid: 52491 topic_id: 10338 reply_id: 37753[/import]

make sure before adding object to physics body you are starting physics

physics = require “physics”

physics.start()

if you add body before this two lines crash occurs

:slight_smile: [import]uid: 12482 topic_id: 10338 reply_id: 37758[/import]

@peach: thanks, but the number I put in the delay doesn’t matter and I think I figured out why. I should be using this command this way:

[lua]timer.performWithDelay(100, action , 1)[/lua]

notice, the action is without an the parameter (e). Now the delay works ok. However, now I need a way to figure out how to pass the event (e) to the action function.
@hgvyas123: all the calls happen after those lines, and function works perfectly if I call it direct, but crashes when called within the collision event. According to the docs this is expected though.

[import]uid: 6942 topic_id: 10338 reply_id: 37770[/import]

did u mean when the collision occurs it is crashing ??

its a strange plz put the sample so someone can see or/and help

thanks [import]uid: 12482 topic_id: 10338 reply_id: 37775[/import]

I got the performWithDelay working, but still the simulator crashes as soon as I add a physics body. So back to the drawing board.

I have created the below script to reproduce the error, notice that I’m also using the director class:

[lua]module(…, package.seeall)
require “sprite”
local localGroup = display.newGroup()
local physics = require “physics”

local mRand = math.random

physics.start( true )
physics.setGravity( 0, 0 )
_H = display.contentHeight;
_W = display.contentWidth;

sheetData = require “sheet@2x”
data = sheetData.getSpriteSheetData()
sheet1 = sprite.newSpriteSheetFromData( “sheet@2x.png”, data )
local sprite1Set = sprite.newSpriteSet(sheet1, 3, 3)
sprite.add( sprite1Set, “sp1”, 1, 3, 2250, -2 )

local sprite2Set = sprite.newSpriteSet(sheet1, 7, 2)
sprite.add( sprite2Set, “sp2”, 1, 2, 1000, -2 )
local function sprite_collide()
local sp = sprite.newSprite(sprite2Set)
sp.x = mRand(80,400)
sp.y = mRand(20,300)
physics.addBody(sp, {density = 0.5 , friction = 0.0, bounce =1,radius = 16})
sp:applyForce(-10+mRand(0,25), -10+mRand(0,25), sp.x,sp.y)

end
local function create_sprites()

for i=1,10,1 do
local other = sprite.newSprite(sprite2Set)
other.x = mRand(80,400)
other.y = mRand(20,300)
physics.addBody(other, {density = 0.5 , friction = 0.0, bounce =1,radius = 16})
other:applyForce(-10+mRand(0,25), -10+mRand(0,25), other.x,other.y)
end

end
function new()

– create physics wall

local bottom_wall = display.newRect(0,0,_W,20)
bottom_wall.y = _H+10
bottom_wall.eName = “wall”
bottom_wall:setStrokeColor(0,0,0,255)
bottom_wall:setFillColor(0,0,0,255)
localGroup:insert(bottom_wall)
physics.addBody(bottom_wall,“static”, {friction = 0.0, bounce = 1})
local top_wall = display.newRect(0,0,_W,20)
top_wall.y = -10
top_wall.eName = “wall”
top_wall:setStrokeColor(0,0,0,255)
top_wall:setFillColor(0,0,0,255)
localGroup:insert(top_wall)
physics.addBody(top_wall,“static”, {friction = 0.0, bounce = 1})

local left_wall = display.newRect(0,0,20,_H)
left_wall.x = -10
left_wall.eName = “wall”
left_wall:setStrokeColor(0,0,0,255)
left_wall:setFillColor(0,0,0,255)
localGroup:insert(left_wall)
physics.addBody(left_wall,“static”, {friction = 0.0, bounce = 1})

local right_wall = display.newRect(0,0,20,_H)
right_wall.x = _W+10
right_wall.eName = “wall”
right_wall:setStrokeColor(0,0,0,255)
right_wall:setFillColor(0,0,0,255)
localGroup:insert(right_wall)
physics.addBody(right_wall,“static”, {friction = 0.0, bounce = 1})

create_sprites() – create 10 sprites

Runtime:addEventListener(“collision”,sprite_collide)

return localGroup

end[/lua] [import]uid: 6942 topic_id: 10338 reply_id: 37784[/import]

got the problem the problem is with ur function sprite_collide()

try this

[lua]local sp = {}

local function addToPhysics(obj)
physics.addBody(obj, {density = 0.5 , friction = 0.0, bounce =1,radius = 16})
obj:applyForce(-10+mRand(0,25), -10+mRand(0,25), sp.x,sp.y)
end

function sprite_collide(event)
if event.phase == “ended” then
sp[#sp + 1] = sprite.newSprite(sprite2Set)
sp[#sp].x = mRand(80,400)
sp[#sp].y = mRand(20,300)
local function callMe()
addToPhysics( sp[#sp])
end
timer.performWithDelay(200,callMe)

end
end[/lua]

:slight_smile: [import]uid: 12482 topic_id: 10338 reply_id: 37788[/import]

Thanks, really appreciated. [import]uid: 6942 topic_id: 10338 reply_id: 37800[/import]

always welcome

:slight_smile: [import]uid: 12482 topic_id: 10338 reply_id: 37875[/import]