Can't make widgets go away

Hi,

I’m really, really new to Lua and Corona, so undoubtedly I’m doing something wrong and wonder if I could get some advice…

I’ve set up a pickerWheel and button to be shown on a release event and when the button is pressed it updates a text field and should remove the picker and button, but it never goes away (I’m using the simulator).

My code goes something like:

local widget = require("widget") local displayClock local displayClockAmPm local pickerSetBtn local pickerWheel local pickerData = {} local pickerHours = {} local pickerMinutes = {} local pickerAmPm = {"am", "pm"} for i = 1, 12 do pickerHours[i] = i end for i = 1, 60 do pickerMinutes[i] = string.format("%02d", i-1) end local function saveTime(hour, minute, ampm) -- set the value of displayClock and displayClockAmPm -- this bit works fine end local function setTimeValue(event) local v = pickerWheel:getValues() local h = v[1].value local m = v[2].value local a = v[3].value saveTime(h, m, a) display.remove(pickerSetBtn) pickerSetBtn = nil display.remove(pickerWheel) pickerWheel = nil end local function initPicker(event) pickerWheel = widget.newPickerWheel{ columns = { { align = "right", startIndex = settings.wakeHour, labels = pickerHours, }, { align = "center", startIndex = settings.wakeMin + 1, labels = pickerMinutes, }, { align = "center", startIndex = (settings.wakeAmPm == "am" and 1 or 2), labels = pickerAmPm, } } } pickerWheel.y = display.contentHeight - pickerWheel.height pickerSetBtn = widget.newButton{ label="Set time", onRelease=setTimeValue } pickerSetBtn:setReferencePoint(display.CenterRightReferencePoint) pickerSetBtn.x = display.contentWidth - 5 pickerSetBtn.y = 22 end local function initClock() displayClock = display.newText("", 0, 0, displayClockFont, 180) displayClockAmPm = display.newText("", 0, 0, displayClockFont, 30) displayClock:addEventListener("touch", initPicker) end initClock()

Any help/advice would be really appreciated - not only on this particular problem but also if you see anything I’m doing that’s completely bonkers. :wink:

Thanks!

Hi @acollington,

Are there any error reports in the Terminal or error log? This may help determine the issue.

Thanks,

Brent

Hi Brent,

Thanks for the speedy response!

No, the terminal doesn’t show any errors unless I try to click on the button again once it should have gone away, then it complains that I’m trying to access something that’s nil, which is understandable.

I haven’t checked any log files - to be honest I don’t know where to locate them (did I mention that I’m really new to Corona? ;-)) I’ll try to track them down on my machine (I’m on a Windows PC of that makes a difference?)

Cheers,

Andy

Ah, I see the problem (I think so). You’re not filtering the “phase” of the touch event which initiates the picker wheel. Thus, you’re actually creating at least 2 of them, if not dozens more (the “moved” phase of a touch can generate dozens or even hundreds of responses in even a short swipe). You should create the widget only on the “ended” phase. Try that and see what happens.

Brent

Excellent!

I added a check for event.phase == “ended” around the code in initPicker and everything worked as it should, with the picker and button going away once I clicked on the button.

Many thanks for your help, Brent!!

Andy

Hi @acollington,

Are there any error reports in the Terminal or error log? This may help determine the issue.

Thanks,

Brent

Hi Brent,

Thanks for the speedy response!

No, the terminal doesn’t show any errors unless I try to click on the button again once it should have gone away, then it complains that I’m trying to access something that’s nil, which is understandable.

I haven’t checked any log files - to be honest I don’t know where to locate them (did I mention that I’m really new to Corona? ;-)) I’ll try to track them down on my machine (I’m on a Windows PC of that makes a difference?)

Cheers,

Andy

Ah, I see the problem (I think so). You’re not filtering the “phase” of the touch event which initiates the picker wheel. Thus, you’re actually creating at least 2 of them, if not dozens more (the “moved” phase of a touch can generate dozens or even hundreds of responses in even a short swipe). You should create the widget only on the “ended” phase. Try that and see what happens.

Brent

Excellent!

I added a check for event.phase == “ended” around the code in initPicker and everything worked as it should, with the picker and button going away once I clicked on the button.

Many thanks for your help, Brent!!

Andy