I am done building my game! or almost, I have a problem that is bugging the heck out of my body. So I want to make a reset button when the balloon hits the floor. Well I got the onCollision to work
local onCollision = function (self, event)
if event.phase == "began" then
if event.other.name == "spikes" then
local gameoverText = display.newText("Game Over!", 0, 0, "HelveticaNeue", 35)
gameoverText:setTextColor(255, 255, 255)
gameoverText.x = display.contentCenterX
gameoverText.y = display.contentCenterY
self:removeSelf()
self = nil
end
end
end
but when I put in the onCollision function a button and set it so the director will change scenes to the game it spits out an error. Any help? [import]uid: 55737 topic_id: 9496 reply_id: 309496[/import]
This is very common and has been answered before, I’m afraid…
You are trying to modify a locked object. The locked object is the body. It is locked because the physics engine is performing an operation on it,in this case the collision operation.
You should encapsulate the code you want to run upon the collision in a closure and call it after 1 millisecond using a timer. Eg:
[lua]local onCollision = function (self, event)
if event.phase == “began” then
if event.other.name == “spikes” then
local gameoverText = display.newText(“Game Over!”, 0, 0, “HelveticaNeue”, 35)
gameoverText:setTextColor(255, 255, 255)
gameoverText.x = display.contentCenterX
gameoverText.y = display.contentCenterY
timer.performWithDelay(1,
function()
self:removeSelf()
self = nil
end,
1)
end
end
end[/lua]
I will admit I’ve not tested this right now, but I’ve used very similar code in my own game to do similar things.
Matt [import]uid: 8271 topic_id: 9496 reply_id: 34740[/import]
I don’t really understand your question, but the error has nothing to do with the button. Also, the button and the onCollision are not really connected. The error is a problem occurring in the physics engine. Separate the code from the button listener and you should be ok. [import]uid: 8271 topic_id: 9496 reply_id: 34771[/import]