restart button

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]

What is your error?
Your button is not showing or director is not changing?

Check out my game:
Touch Cannon – a **FREE** game

Please rate :slight_smile: [import]uid: 12455 topic_id: 9496 reply_id: 34717[/import]

my director is not changing.
the code I tried using is

director:changeScene(“game”)

Game is the scene which is game is in [import]uid: 55737 topic_id: 9496 reply_id: 34721[/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]

So I add the director code and link it to the button code via addEventListener [import]uid: 55737 topic_id: 9496 reply_id: 34766[/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]