Crash on collision

I really need help right now… Been pulling my hair for a while
Ever since I downloaded the latest build of Corona (312) bad code has been exposed. Code that worked in previous versions of Corona due to bugs now don’t work, so I’ve been trying to fix it for 2 whole days… It’s the last thing I’ve got to fix before shipping my app so I realllly want to get this done, please I’d appreciate all help! [import]uid: 14018 topic_id: 7813 reply_id: 307813[/import]

And this is the spawn enemy function:

  
-- Spawn funktionen  
local enemy  
local enemyNumber  
  
local function choose1()  
  
 local choose = rnd(1, 2)  
  
 -- If satsen  
 if(choose == 1) then  
  
 if(ballSize \<= 8) then  
 enemyNumber = rnd(1, 3)  
 elseif(ballSize \> 8 and ballSize \<= 18) then  
 enemyNumber = rnd(3, 6)  
 end  
  
 enemy = display.newImage(""..enemyNumber..".png", rnd(228, 230), -140)  
 localGroup:insert(enemy)  
 worldGroup:insert(enemy)  
  
 -- Radius  
 if(enemyNumber == 1) then  
 enemySize = 4  
 enemy.class = "1"  
 elseif(enemyNumber == 2) then  
 enemySize = 5  
 enemy.class = "2"  
 elseif(enemyNumber == 3) then  
 enemySize = 6  
 enemy.class = "3"  
 elseif(enemyNumber == 4) then  
 enemySize = 7  
 enemy.class = "4"  
 elseif(enemyNumber == 5) then  
 enemySize = 8  
 enemy.class = "5"  
 elseif(enemyNumber == 6) then  
 enemySize = 9  
 enemy.class = "6"  
 end  
  
  
  
 -- Fysik  
 physics.addBody(enemy, { density=1.0, friction=0.3, bounce=0,   
 radius=enemySize, isSensor=true })  
  
 if(enemyNumber == 1) then  
 enemy:applyForce(0, rnd(3, 7), 0, 0)  
 elseif(enemyNumber == 2) then  
 enemy:applyForce(0, rnd(5, 9), 0, 0)  
 elseif(enemyNumber == 3) then  
 enemy:applyForce(0, rnd(7, 17), 0, 0)  
 elseif(enemyNumber \> 3 and enemyNumber \<= 5) then  
 enemy:applyForce(0, rnd(26, 56), 0, 0)  
 elseif(enemyNumber \> 5 and enemyNumber \<= 6) then  
 enemy:applyForce(0, rnd(35, 65), 0, 0)  
 end  
  
 -- Opacy  
 enemy.alpha = 0.7  
  
 -- Lägg in i grupp  
 enemyGroup:insert(enemy)  
  
 -- Ingen rotation  
 enemy.isFixedRotation = true  
  
 -- Is Collideable  
 enemy.isCollideable = true  
  
 local function enemyCollision(self, event)  
 if(event.phase == "began" and event.other.class == "enemyCleaner") then  
  
 self:removeSelf()  
 self = nil  
  
 elseif(event.phase == "began" and event.other.class == "eastWall") then  
  
 self:removeSelf()  
 self = nil  
  
 end  
  
 return true  
 end  
 enemy.collision = enemyCollision  
 enemy:addEventListener("collision", enemy)  
  
  
  
 -- Slut på If satsen  
 end  
  
end  
local enemyTimer = performAfterDelay(0.3, choose1, 0, true)  

And again, if I remove the ‘return true’ from the enemyCollision-function, the simulator will crash on collision. But this time, with return true it works all the time
Thanks in advance! [import]uid: 14018 topic_id: 7813 reply_id: 27722[/import]

It crashes right on collision. If I remove everything that has anything to do with event.other and if i remove ‘ball:removeSelf() and ball = nil’ it works flawlessly… And if I don’t remove anything but just add a return true statement right before the end of the function it works to some degree… Sometimes the collision will work and sometimes it wont

[code]
local ballCollisionFilter = { categoryBits=1, maskBits=3 }

local ball
local createBallOnce = true
local ballSize = 7

local function createBall(xpos, ypos)

ball = display.newImage(“ball.png”)
localGroup:insert(ball)
worldGroup:insert(ball)

if(createBallOnce == true) then

tm:add(ball, { time=500, xScale=(ball/90), yScale=(ball/90),
transition=easingx.easeOutElastic })

end

ball.xScale = ballSize/90
ball.yScale = ballSize/90

if(createBallOnce == false) then
transition.from(ball, { time=150, xScale=(ballSize/90)*2, yScale=(ballSize/90)*2 })
else
transition.from(ball, { time=100, xScale=(ballSize/90)*4, yScale=(ballSize/90)*4 })

createBallOnce = false
end

ball.x = xpos
ball.y = ypos

ball.hasHadCollision = false

– Class
ball.class = “ball”

physics.addBody(ball, “dynamic”, { density=0.4, friction=10, bounce=0,
radius=ballSize, filter=ballCollisionFilter, isSensor=true })

ball.isFixedRotation = true
ball.isSleepingAllowed = false

local function collisionWatcher()
if(ball.hasHadCollision == true) then
Runtime:removeEventListener(“enterFrame”, ball)
ball:removeEventListener(“collision”, ball)

local newX = ball.x
local newY = ball.y

ball:removeSelf()
ball = nil

createBall(newX, newY)
end

end
ball.enterFrame = collisionWatcher
Runtime:addEventListener(“enterFrame”, ball)

– Kollision
local function onCollisionBall(self, event)
if(event.phase == “began” and event.other.class == “1”) then
if(event.other.isCollideable == true) then

globalScore = globalScore + 14
showScore.text = globalScore
showScore:setReferencePoint(display.TopLeftReferencePoint)
showScore.x = 14
showScore.y = 8

showScore.xScale = 1.4
showScore.yScale = 1.4
showScore:setReferencePoint(display.CenterReferencePoint)
transition.to(showScore, { time=100, xScale=1, yScale=1 })

ball.hasHadCollision = true

event.other:removeSelf()
event.other = nil

end

end

end
ball.collision = onCollisionBall
ball:addEventListener(“collision”, ball)

end
createBall(240, 280)
[/code] [import]uid: 14018 topic_id: 7813 reply_id: 27721[/import]

bump [import]uid: 14018 topic_id: 7813 reply_id: 27775[/import]

you’re trying to create a new ball (and hence physics body) *inside* your collision function, i very much doubt that will work without crashes. you can’t change the physics system during a collision [import]uid: 6645 topic_id: 7813 reply_id: 27780[/import]

No I’m flagging it on collision and outside the collision function I’ve got an enterframe function checking the flag - creating a new ball… [import]uid: 14018 topic_id: 7813 reply_id: 27790[/import]

sorry, yes you are, I read it wrong [import]uid: 6645 topic_id: 7813 reply_id: 27809[/import]

i would do this …

[lua]local function collisionWatcher()
if(ball.hasHadCollision == true) then

– you need to add this line
ball.hasHadCollision=false – [/lua]

that stops it trying to run it more times than it needs to at least

i dont know if it will fix your problem but it definitely needs to be reset at some point. i know it does when you recreate your ball but i’m not sure if that is soon enough. if you want to put a zip on mediafire (or wherever) i will have a play at some point [import]uid: 6645 topic_id: 7813 reply_id: 27810[/import]