Simulator crashes then sprite collide with ground

I have a little game there the simulator crashes then the ship collides with the ground, but sometimes it runs fine, may this be something with the simulator or is it bad coding?

It often happens if i press a move button at the same time as the ship has the collision.

my function that checks for collisions looks like this:

[lua]–A function that checks collisions both if the ufo has landed and if it has crashed
function collisionWithObstacle(event)

vex, vey = spriteInstance:getLinearVelocity()
print(vey)

–An if that checks if the ufo collides with the platform and if
–that is the case it will stop moving and the text “Grattis du landade”
–appears in the middle of the screen
if(event.other.name == “platform” and vey < 100) then

local landat = display.newText(“Grattis du landade!”, 50, 150, native.systemFont, 40)
crashed = true
spriteInstance.linearDamping = 100,0

elseif (event.other.name == “platform” and vey > 100) then

spriteInstance.isBodyActive = false
spriteInstance:prepare(“explosion”)
spriteInstance:play(“explosion”)

–An elseif that checks if the ufo collides with the gorund if it does then the
–ufo will disappear and an explosion will appear
elseif (event.other.name == “ground”) then

spriteInstance.isBodyActive = false
spriteInstance:prepare(“explosion”)
spriteInstance:play(“explosion”)

end

end

–An listener who listens after collisons
spriteInstance:addEventListener(“collision”, collisionWithObstacle)[/lua]

and the function for moving the ship :

[lua]–A function the controls the ufos movement
function ufoMovement(event)

–An if that controls what happens then user hit anywhere on the screen and it goes up
if(event.target.name == “up” and crashed == false) then

–An if that fires an timer then touch began
if(event.phase == “began”) then

tmrUp = timer.performWithDelay(150, engines, 0)

–An elseif that checks then user stops to touch the button
elseif(event.phase == “ended”) then
timer.cancel(tmrUp)

spriteInstance.linearDamping = 0

–An elseif that checks if ufo has crashed then makes spriteInstance the crashedPos
elseif(crashed == true) then

spriteInstance.x = crashedPos

end

end

–An if that controls what happens then user hit the left button and it goes left
if(event.target.name == “left” and crashed == false) then

–An if that fires an timer then touch began
if(event.phase == “began”) then

tmrLeft = timer.performWithDelay(200, sideleft, 0)

–An elseif that checks then user stops to touch the button
elseif(event.phase == “ended”) then

timer.cancel(tmrLeft)

–An elseif that checks if ufo has crashed then makes spriteInstance the crashedPos
elseif(crashed == true) then

spriteInstance.x = crashedPos

end

end

–An if that controls what happens then user hit the right button and it goes right
if(event.target.name == “right” and crashed == false) then

–An if that fires an timer then touch began
if(event.phase == “began”) then

tmrRight = timer.performWithDelay(150, sideright, 0)

elseif(event.phase == “ended”) then

timer.cancel(tmrRight)

–An elseif that checks if ufo has crashed then makes spriteInstance the crashedPos
elseif(crashed == true) then

spriteInstance.x = crashedPos

end

end

end
–A function with a timer in that applys force to the sprite up
function engines(event)
spriteInstance.linearDamping = 0.5
spriteInstance:applyForce(0, -30, spriteInstance.x, spriteInstance.y)
end

–A function with a timer in that applys force to the sprite left
function sideleft(event)

spriteInstance:applyForce(-20, 0, spriteInstance.x, spriteInstance.y)

end

–A function with a timer in that applys force to the sprite right
function sideright(event)

spriteInstance:applyForce(20, 0, spriteInstance.x, spriteInstance.y)

end
–Three listeners that listen for touch on the different buttons in the game
buttonUp:addEventListener(“touch”, ufoMovement)
buttonLeft:addEventListener(“touch”, ufoMovement)
buttonRight:addEventListener(“touch”, ufoMovement)[/lua]

I am using the director class don’t know if that has anything to do with it?

Hope to get som pointers

Kind regards Inkoqnito [import]uid: 90942 topic_id: 16693 reply_id: 316693[/import]

Hey.

Off the top of my head I think it’s because you are changing something on “spriteInstance” at the same time as box2D is calculating the collision.

The Collision detection documentation mentions this, but a simple work around is to add a function, then call it on a timer after a millisecond or so, (so they don’t happen at exactly the same time. Something like this.

 if(event.other.name == "platform" and vey \< 100) then  
  
 local function delayIt()  
  
 local landat = display.newText("Grattis du landade!", 50, 150, native.systemFont, 40)  
 crashed = true  
 spriteInstance.linearDamping = 100,0  
 end  
  
 Timer1 = timer.performWithDelay(1,delayIt,1)   

Hope that helps.
[import]uid: 67933 topic_id: 16693 reply_id: 62443[/import]

Hey Spider_newgent!

Thanks for the answer!

And thats exactly what was wrong sat and tested it when i saw your post=)

Read in Beebes blog about it, ansca should build in a fix for this backend, so i don’t have to think about it:P

Thanks for the help anyway:)
Take care Inkoqnito [import]uid: 90942 topic_id: 16693 reply_id: 62445[/import]

Glad I could help! [import]uid: 67933 topic_id: 16693 reply_id: 62494[/import]