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]