Tie action of one button to another button

Can you tie the action of one button to another button? I have a native alert button that says Play Again. I then have another button that is a reset button, which resets my game board. How do I tie the function of one button to another button?

This is the code for the Native Alert.

[lua] – Handler that gets notified when the alert closes
local function onComplete( event )
if “clicked” == event.action then
local i = event.index
if 1 == i then
director:changeScene( “titlescreen”, “fade” )
elseif 2 == i then
– Return to gameboard and Reset game
–I would think the code goes here?

end
end
end
This is the code for the reset button

[lua]local function resetbuttonfn (event)
physics.setGravity(0,9.8)
director:changeScene( “gameboard”, “flip” )
media.playEventSound( “bloop_x.caf” )
end

resetbutton:addEventListener (“tap”, resetbuttonfn)[/lua] [import]uid: 72372 topic_id: 12941 reply_id: 312941[/import]

I’m not totally sure what you’re asking - do you mean that you want another button to also call the same function? [import]uid: 52491 topic_id: 12941 reply_id: 47466[/import]

Yes now that I’m thinking about it yes that’s it in more simple terms:-) I want the native alert button that says Play again to reset the gameboard when it is pushed. [import]uid: 72372 topic_id: 12941 reply_id: 47469[/import]

Just add the event listener onto the other button and it will perform the exact same way.

[lua]playagainbutton:addEventListener (“tap”, resetbuttonfn)[/lua] [import]uid: 31262 topic_id: 12941 reply_id: 47486[/import]

Hey Peach,

I tried that but it didn’t work. I’ll keep trying. this is the code though with what you said.

[lua]-- Handler that gets notified when the alert closes
local function onComplete( event )
if “clicked” == event.action then
local i = event.index
if 1 == i then
director:changeScene( “titlescreen”, “fade” )
elseif 2 == i then
– Return to gameboard and Reset game
playagainbutton:addEventListener (“tap”, resetbuttonfn)

end
end
end[/lua] [import]uid: 72372 topic_id: 12941 reply_id: 47493[/import]

try this instead

-- Handler that gets notified when the alert closes  
local function onComplete( event )  
 if "clicked" == event.action then  
 local i = event.index  
 if 1 == i then  
 director:changeScene( "titlescreen", "fade" )  
 elseif 2 == i then  
 -- Return to gameboard and Reset game  
 resetbuttonfn()  
  
 end  
 end  
end  

[import]uid: 43961 topic_id: 12941 reply_id: 47512[/import]

Brett,

That didn’t work either:-( [import]uid: 72372 topic_id: 12941 reply_id: 47524[/import]

did you get an error?

try resetbuttonfn(event) [import]uid: 43961 topic_id: 12941 reply_id: 47525[/import]

I didn’t get an error on either of the items you advised to type. It just didn’t do anything. [import]uid: 72372 topic_id: 12941 reply_id: 47528[/import]

ok make sure that your code for this:

local function resetbuttonfn (event)
blah
end
is above the on complete handler…
[import]uid: 43961 topic_id: 12941 reply_id: 47529[/import]

actually that still won’t work, because you have the

if “clicked” == event.action

section.

try adding a print(“event received”) to the oncomplete handler before the if “clicked” == event.action

and see if it fires [import]uid: 43961 topic_id: 12941 reply_id: 47530[/import]

If you mean like this. It didn’t work either and there was no error.

[lua]-- Handler that gets notified when the alert closes
local function onComplete( event )
print(“event received”)
if “clicked” == event.action then
local i = event.index
if 1 == i then
director:changeScene( “titlescreen”, “fade” )
elseif 2 == i then
– Return to gameboard
resetbuttonfn(event)

end
end
end[/lua] [import]uid: 72372 topic_id: 12941 reply_id: 47531[/import]

The following works for me

local function resetbuttonfn (event)  
print("boing")  
  
end   
-- Handler that gets notified when the alert closes  
local function onComplete( event )  
 if "clicked" == event.action then  
 local i = event.index  
 if 1 == i then  
 print("menu")  
  
 elseif 2 == i then  
 print("reset")  
 resetbuttonfn(event)  
  
 end  
 end  
end  
  
local alert = native.showAlert( "Choose", "Choice",   
 { "Play Again", "Reset" }, onComplete )  
-- notice I've added this event listener to to runtime just for this quick example  
Runtime:addEventListener ("tap", resetbuttonfn)  
  

[import]uid: 43961 topic_id: 12941 reply_id: 47533[/import]

Not sure what’s wrong but that doesn’t work for me. It is given an error now and I can’t get to the board.

Michelle

[import]uid: 72372 topic_id: 12941 reply_id: 47542[/import]