device crash on collision / change scene

Well,
I thought everything was fine and dandy and my game would be up on googleplay now but alas how wrong could I bee( not a spelling mistake).

I will strip this down to its simplest form.
An object collides with another 5 times, on the fifth time the health bar is depleted, a game over function is called as is a change scene function.
Issue is that on the fifth collision the game crashes and returns to the phones desktop.
I have a countdown tiwner also that when it reaches zero it calls the game over function and then the change scene function all working fine. So I guess the gameover function is doing its job.
Logcat gives me a fatal signal 11 SIGSEGV fault, great…

The object is infact an animated sprite which collides with another animated sprite, a single frame is played at collision before the end of the sequence.

I am thinking it may be a timer/transition problem.i.e not cancelling but I am sure I have covered all this and cancelled and destroyed all objects before changing scenes.
I am thinking it has to be something in the way the 2 sprites react, but I am struggling to find the answer.
All the code is fresh and not borrowed from anywhere

I know its difficult to diagnose, what I am looking for are ideas please?
[import]uid: 127675 topic_id: 31700 reply_id: 331700[/import]

Hi there,

Box2D will crash if you try to remove a physics object while it is in the midst of a collision. See the section labeled “Important Restriction” on this page: http://developer.coronalabs.com/content/game-edition-collision-detection.

What you can do is enclose your bee removal into a timer.performWithDelay, with a very short delay so that it’s not noticeable to the player, like this:


timer.performWithDelay(1, function()
bee:removeSelf()
bee=nil
end
)

Hope this helps.

  • Andrew [import]uid: 109711 topic_id: 31700 reply_id: 126639[/import]

@aukStudios,

thanks for that, I seem to have read that ages ago but forgot about it.
Well I tried implement your suggestions and yes I can work with it but I am still getting that crash.
It is so frustrating.
I think its because I have several timers for the bee; for example this is my scenario:

Bee spawns and transitions to x,y every 5 seconds.

local beeSpawnTimer = timer.performWithDelay(5000, beeSpawn, 12)  

Bee hangs around for extra 2 seconds when the transition is complete, total 7 seconds.

local removeBeeTimer = timer.performWithDelay( 7000, removeBee)  

If a collision occurs during transition I remove the timer,( in the collision detection) it stops the physics body remaining there.

if removeBeeTimer then timer.cancel ( removeBeeTimer ); removeBeeTimer = nil  

Still struggling though.
Can I have collision detection filters on a non physics body?

thanks

[import]uid: 127675 topic_id: 31700 reply_id: 126649[/import]

Hi there,

First to answer your question: no, you can’t have collision detection (including collision filters as well as any other collision functionality) on non-physics bodies.

I’m confused when you say it’s still crashing. In your first post, you said the your stripped down code worked fine, and it only crashed when you add the code to remove the bee in the collision. By enclosing the bee’s removal in a brief delay, it should eliminate the crash. Is that not the case?

I’d be interested to see a new version of your stripped down code that still crashes.

  • Andrew [import]uid: 109711 topic_id: 31700 reply_id: 126680[/import]

Strange that it is crashing on the device but not the simulator.

In cases like this, I would recommend inserting lots of print statements, practically after every line if needed. Then, run the game on your device while it’s connected to your computer. If it’s an iOS device, you can see the output of the print statements in the xCode console. The last print statement to print will be just before the crash.

  • Andrew [import]uid: 109711 topic_id: 31700 reply_id: 126703[/import]

@aukStudios,

thanks for the advice.
I am working with android devices, the crash happens on htc one x, desire hd, nexsus and samsung ace.

Ok, so I have planted 50 print statements in the bee code.
I have loaded logcat up and ran the game.

I notice the following:

clean function called-- from game, print statement received
“continues too”
gameover scene – print statement received
“continues to”
director:changeScene(‘gameover_scene’) – print statement received
“continues to”
bee removed with delay – print statement received from game

then crashes.

Seems a problem with timers, but I just cant see it.
Anyway time to plod on.

[import]uid: 127675 topic_id: 31700 reply_id: 126706[/import]

Hi there,

Box2D will crash if you try to remove a physics object while it is in the midst of a collision. See the section labeled “Important Restriction” on this page: http://developer.coronalabs.com/content/game-edition-collision-detection.

What you can do is enclose your bee removal into a timer.performWithDelay, with a very short delay so that it’s not noticeable to the player, like this:


timer.performWithDelay(1, function()
bee:removeSelf()
bee=nil
end
)

Hope this helps.

  • Andrew [import]uid: 109711 topic_id: 31700 reply_id: 126639[/import]

@aukStudios,

thanks for that, I seem to have read that ages ago but forgot about it.
Well I tried implement your suggestions and yes I can work with it but I am still getting that crash.
It is so frustrating.
I think its because I have several timers for the bee; for example this is my scenario:

Bee spawns and transitions to x,y every 5 seconds.

local beeSpawnTimer = timer.performWithDelay(5000, beeSpawn, 12)  

Bee hangs around for extra 2 seconds when the transition is complete, total 7 seconds.

local removeBeeTimer = timer.performWithDelay( 7000, removeBee)  

If a collision occurs during transition I remove the timer,( in the collision detection) it stops the physics body remaining there.

if removeBeeTimer then timer.cancel ( removeBeeTimer ); removeBeeTimer = nil  

Still struggling though.
Can I have collision detection filters on a non physics body?

thanks

[import]uid: 127675 topic_id: 31700 reply_id: 126649[/import]

Hi there,

First to answer your question: no, you can’t have collision detection (including collision filters as well as any other collision functionality) on non-physics bodies.

I’m confused when you say it’s still crashing. In your first post, you said the your stripped down code worked fine, and it only crashed when you add the code to remove the bee in the collision. By enclosing the bee’s removal in a brief delay, it should eliminate the crash. Is that not the case?

I’d be interested to see a new version of your stripped down code that still crashes.

  • Andrew [import]uid: 109711 topic_id: 31700 reply_id: 126680[/import]

Ok,
I have removed this post as I can explain much better lower down as I found a solution. [import]uid: 127675 topic_id: 31700 reply_id: 126636[/import]

@aukStudios,

yes, I stripped the code down to a minimum and yes after inserting
the code it still does the same.
I placed print statements in the code to make certain it was firing, I also lengthened the delay and used hybrid mode to view the physics body.

see resolution below [import]uid: 127675 topic_id: 31700 reply_id: 126701[/import]

Right then,

I was having a problem when I called a game over function after a final collision in which the game ended.

Basically, the game ends either when the timer runs out or you take 5 collisions.

The game would quite happily switch to the gameover scene ( I am using a stripped down version of director) once the timer had ran out; the issue as stated above was the last collision of the five which called the gameover function would cause a crash in the device. there were no errors in the simulator.

Now we know box2d has an issue when stopping transitions, it causes crashes as stated above.
I thought I would include lots and lots of print statements in order to see where the crash happened.
Armed with this and logcat I began another debug session.
Looking at the logcat output I thought something was wrong; I noticed the changescene function would be called then noticed another reference to the previous scene and the the clean function.

I went through all the timers/transitions etc again and again but couldnt put my finger on it.
I did a little more research and began to read the director to storyboard transition post.
Well as soon as I had read this I knew that the logcat output was correct.

I didnt realise the next scene is prepared before the current is cleaned etc, therefore it became clear.
I just did so:

if lives == 0 then  
  
 gameOver(event)  
 print('gameover function called')  
 timer.performWithDelay(500,function()director:changeScene('scene\_gameover')end)  
  

by delaying the transition from game scene to gameover scene all worked perfectly on the devices.

This gives time for the transitions and timers I guess to respond. I put a 10 second delay on the scene change originally this slowed everything right down; it helped a lot.
I am still going through the code in order to see what exactly is happening, but feel I am nearly there.

Also dont know if this should be moved to the director area.

Rock on !

[import]uid: 127675 topic_id: 31700 reply_id: 126875[/import]

Strange that it is crashing on the device but not the simulator.

In cases like this, I would recommend inserting lots of print statements, practically after every line if needed. Then, run the game on your device while it’s connected to your computer. If it’s an iOS device, you can see the output of the print statements in the xCode console. The last print statement to print will be just before the crash.

  • Andrew [import]uid: 109711 topic_id: 31700 reply_id: 126703[/import]

@aukStudios,

thanks for the advice.
I am working with android devices, the crash happens on htc one x, desire hd, nexsus and samsung ace.

Ok, so I have planted 50 print statements in the bee code.
I have loaded logcat up and ran the game.

I notice the following:

clean function called-- from game, print statement received
“continues too”
gameover scene – print statement received
“continues to”
director:changeScene(‘gameover_scene’) – print statement received
“continues to”
bee removed with delay – print statement received from game

then crashes.

Seems a problem with timers, but I just cant see it.
Anyway time to plod on.

[import]uid: 127675 topic_id: 31700 reply_id: 126706[/import]

Ok,
I have removed this post as I can explain much better lower down as I found a solution. [import]uid: 127675 topic_id: 31700 reply_id: 126636[/import]

@aukStudios,

yes, I stripped the code down to a minimum and yes after inserting
the code it still does the same.
I placed print statements in the code to make certain it was firing, I also lengthened the delay and used hybrid mode to view the physics body.

see resolution below [import]uid: 127675 topic_id: 31700 reply_id: 126701[/import]

Right then,

I was having a problem when I called a game over function after a final collision in which the game ended.

Basically, the game ends either when the timer runs out or you take 5 collisions.

The game would quite happily switch to the gameover scene ( I am using a stripped down version of director) once the timer had ran out; the issue as stated above was the last collision of the five which called the gameover function would cause a crash in the device. there were no errors in the simulator.

Now we know box2d has an issue when stopping transitions, it causes crashes as stated above.
I thought I would include lots and lots of print statements in order to see where the crash happened.
Armed with this and logcat I began another debug session.
Looking at the logcat output I thought something was wrong; I noticed the changescene function would be called then noticed another reference to the previous scene and the the clean function.

I went through all the timers/transitions etc again and again but couldnt put my finger on it.
I did a little more research and began to read the director to storyboard transition post.
Well as soon as I had read this I knew that the logcat output was correct.

I didnt realise the next scene is prepared before the current is cleaned etc, therefore it became clear.
I just did so:

if lives == 0 then  
  
 gameOver(event)  
 print('gameover function called')  
 timer.performWithDelay(500,function()director:changeScene('scene\_gameover')end)  
  

by delaying the transition from game scene to gameover scene all worked perfectly on the devices.

This gives time for the transitions and timers I guess to respond. I put a 10 second delay on the scene change originally this slowed everything right down; it helped a lot.
I am still going through the code in order to see what exactly is happening, but feel I am nearly there.

Also dont know if this should be moved to the director area.

Rock on !

[import]uid: 127675 topic_id: 31700 reply_id: 126875[/import]