Help on crash when an object is on drag.

Hello. Ok so this is what I’m doing. You know the game http://itunes.apple.com/us/app/stay-alive/id488838564?mt=8
. Well if anyone played it and saw how the controls are. Yeah that is what I’m trying to do for my game is have the plane move up and down only. But my problem is when I do that the game crashes after the plane reaches 0 lives. The only way it crashes is if I it drag fast and make it loose life quick it crashes. If I drag it gently it does not crash (someitmes). Here is the code how I have it

[code]local canBeDragged = true

local function startDrag( event ) --This here is the drag code
local t = event.target

local phase = event.phase
if canBeDragged == true then
if “began” == phase then
display.getCurrentStage():setFocus( t )
t.isFocus = true

– Store initial position
–t.x0 = event.x - t.x
t.y0 = event.y - t.y

– Make body type temporarily “kinematic” (to avoid gravitional forces)
event.target.bodyType = “static”

elseif t.isFocus then
if “moved” == phase then
–t.x = event.x - t.x0
t.y = event.y - t.y0

elseif “ended” == phase or “cancelled” == phase then
display.getCurrentStage():setFocus( nil )
t.isFocus = false

– Switch body type back to “dynamic”, unless we’ve marked this sprite as a platform
if ( not event.target.isPlatform ) then
event.target.bodyType = “static”
end

end
end
end

– Stop further propagation of touch event!
return true
end

local sheet1 = sprite.newSpriteSheet( “work4.png”, 92, 63 ) --Here
– Defining the sprite we’re going to be using
local heroset = sprite.newSpriteSet (sheet1, 1, 7)
sprite.add (heroset, “hero”, 1, 7, 200, 0)

local heli = sprite.newSprite (heroset)
heli.x = 50
heli.y = 100
heli.hit = “heli”
physics.addBody(heli ,“static”,{radius = 20})
localGroup:insert(heli)
heli:prepare(“hero”)
heli:play()
heli:addEventListener( “touch”, startDrag ) —To here is the helicopter/plane

local function dead () —This code right here is just for when lives is 0
if Life <= 0 then
great = true
canBeDragged = 0
Runtime:removeEventListener( “enterFrame”, startDrag )
Runtime:removeEventListener(“enterFrame”, wrap)
Runtime:removeEventListener(“enterFrame”, move2)
Runtime:removeEventListener(“enterFrame”, moveship)
timer.cancel (bad1)
timer.cancel (bad2)
timer.cancel (bad3)
timer.cancel (bad31)
timer.cancel (bad4)
timer.cancel (move3)
timer.cancel (move2)
timer.cancel (move1)
timer.cancel (move31)
timer.cancel (move21)
timer.cancel (move11)
local function fixit (event)
director:changeScene (“over”)
end
timer.performWithDelay (1000,fixit,1)
end
end
heli:addEventListener (“collision”, dead)[/code]

So what could be the problem?
I just want the control to like Stay Alive because I find it easier for the game control to work efficient. I’m trying to copy. I hope this is enough coding to solve it.

[import]uid: 17058 topic_id: 23034 reply_id: 323034[/import]

Try isolating.

What happens when the plane reaches 0 lives? How about just trying to print “dead” and seeing if it crashes then? If not you know to start moving forward, adding bits of code back in, until you find the one responsible. [import]uid: 52491 topic_id: 23034 reply_id: 92126[/import]

@peach I got thanks it thanks, wow I’m so dumb the problem was I was supposed to remove this

Runtime:removeEventListener("enterFrame", wrap) Runtime:removeEventListener("enterFrame", moveship)

It never gave me an error in the terminal about that but I remember I was not using those listener so those never existed in the code. Thanks for the adivce and help

:slight_smile: [import]uid: 17058 topic_id: 23034 reply_id: 92151[/import]

Great job, well done for tracking it down and fixing it. Is great to see! :slight_smile: [import]uid: 52491 topic_id: 23034 reply_id: 92324[/import]