event handling - Corona SDK - Cancel timer from within Widget Candy button onPress handler -


i trying cancel timer within onpress event handler of widget candy button. however, timerid nil though have defined local variable within file scope. new lua development assuming issue has variable scope having difficulty figuring out way access timer variable without making true global variable (i.e. declaring without "local" keyword). see code snippet below.

local countdowntimer = timer.performwithdelay(1000, handlecountdowntimer, countdown);  local answerbutton1 = wcandy.newbutton { x       = "center", y       = "55%", width   = "75%", name    = "mybutton1", theme   = "theme_1", border     = {"normal",6,1, .12,.12,0,.4,  .72,.72,.72,.6}, presscolor = {1,1,1,.25}, caption = "touch me!", textalign   = "center", fontsize    = "40", onpress     =   function( eventdata )                     timer.cancel(countdowntimer); -- "countdowntimer" nil!!!                     local button = wcandy.gethandle("mybutton1");                     if button:get("caption") == tostring(solution)                         questiontext:set("caption", "correct!");                     else                         questiontext:set("caption", "wrong!");                     end                 end } 

i see nothing wrong code posted: countdowntimer should non-nil when onpress gets called. should not have test if countdowntimer nil. print countdowntimer after create confirm not nil before create new button. search code find places variable referenced , check if of them set nil.

update:

one thing try moving function , make local:

local countdowntimer = timer.performwithdelay(1000, handlecountdowntimer, countdown);  local function canceltimer( eventdata )     timer.cancel(countdowntimer); -- "countdowntimer" nil!!!     local button = wcandy.gethandle("mybutton1");     if button:get("caption") == tostring(solution)         questiontext:set("caption", "correct!");     else         questiontext:set("caption", "wrong!");     end end  local answerbutton1 = wcandy.newbutton { x       = "center", y       = "55%", width   = "75%", name    = "mybutton1", theme   = "theme_1", border     = {"normal",6,1, .12,.12,0,.4,  .72,.72,.72,.6}, presscolor = {1,1,1,.25}, caption = "touch me!", textalign   = "center", fontsize    = "40", onpress     = canceltimer } 

update 2 (after @husterk found above still doesn't work):

i can't see wrong have tested in corona sim , there no problem it, issue elsewhere. here main.lua in entirety:

local widget = require('widget')  local function handlecountdowntimer(event)     print('timed out; timer:', event.source) end  local countdowntimer  local function canceltimer( event )     print('cancelling timout, timer:', countdowntimer)     timer.cancel(countdowntimer)  end  local answerbutton1 = widget.newbutton {     onpress     = canceltimer,      width = 100,     height = 100,     label = 'hello',     emboss = true }  countdowntimer = timer.performwithdelay(10000, handlecountdowntimer) print('timer started:', countdowntimer) 

Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -