Function causes a crash

Hello

I have two functions, both work in the same way, but one causes the simulator to crash.

I’m trying to using physics.addBody and this is what causes it to happen. Basically the code is designed to make an object called “bullet” disappear from the screen and reappear elsewhere when it hits a certain item on the screen. This code below is called using the event listener shown also below.

  
function transportBullet(event)  
 bullet:removeSelf();  
 bullet = display.newImage('bullet.png')  
 bullet:setReferencePoint(display.CenterReferencePoint);  
 bullet.x = 10  
 bullet.y = 10  
  
 physics.addBody(bullet, { density=2.0, friction=0, bounce=0.5, radius=9 } )  
end  
 object3:addEventListener('collision',transportBullet)  

I’m guessing there is an easier way to do this - any ideas why this happens or how to solve it?

Thanks! [import]uid: 45932 topic_id: 9201 reply_id: 309201[/import]

If you don’t start the physics (physics.start()) before adding any kind of body, you get an instant crash.

And why exactly are you removing it? Just move it. [import]uid: 51516 topic_id: 9201 reply_id: 33571[/import]

Hi seth, thanks for your reply.

The physics is started, I just didn’t add that bit to the forum.

I’m not sure why I’m removing it - that’s why I asked if there’s an easier way :slight_smile:

How do I set it to move to the new coordinates?

[import]uid: 45932 topic_id: 9201 reply_id: 33573[/import]

Well, I didn’t have to move a physics body by hand after adding it, but I’m guessing (and it should be that way!) that moving the display object should move the attached body.

[lua]function transportBullet(event)
bullet.x = 10
bullet.y = 10
end[/lua] [import]uid: 51516 topic_id: 9201 reply_id: 33574[/import]

Hmmm, that doesn’t work. I’d already tried that.

I get a message upon collision in the terminal saying :

“ERROR: Cannot translate an object before collision is resolved”

I tried adding if event.phase is ended but that doesn’t help either. [import]uid: 45932 topic_id: 9201 reply_id: 33575[/import]

I see. Let’s think outside the box:

[lua]function transportBullet(event)
bullet.isSensor = true
bullet.x = 10
bullet.y = 10
timer.performWithDelay( 1000, function() bullet.isSensor = false; end )
end[/lua] [import]uid: 51516 topic_id: 9201 reply_id: 33577[/import]

If this doesn’t work, register for “postCollision” insted of “collision”. Even if it works actually. [import]uid: 51516 topic_id: 9201 reply_id: 33578[/import]

Thanks for the reply! Sorry for the delay in getting back to you - the sun was out and I swapped the laptop for a few beers.

Neither of these are working either though. postCollision results in the same error message. The other way allows the ball to travel straight through the object.

Reading more on the forums, it seems other people had the same problems.

http://developer.anscamobile.com/forum/2011/02/20/postcollision-does-not-fire-after-collision-has-been-completely-resolved [import]uid: 45932 topic_id: 9201 reply_id: 33812[/import]

hmmm… can you post more code?
Not sure if this is what you have in mind, but try this:

[lua]local onCollide = function (self, event)
if event.phase == “began” then
– cancel any Runtime for the object if any
local objTran = transition.to(self, {time = 1, x = 20, y = 20})
end
end [import]uid: 12455 topic_id: 9201 reply_id: 33997[/import]

Hi teex84

Thanks for this, I’ll give it a go and get back to you!

Steve [import]uid: 45932 topic_id: 9201 reply_id: 34079[/import]