我对将 OOP 应用于 Corona 有一点心理障碍。我创建了一个时间选择器类,我希望在我的应用程序中多次重复使用它。我尝试将它的两个实例放在同一个故事板场景中,但第一个选择器上的按钮控制数据并在第二个选择器上显示。所以他们正在共享数据、功能等。我想知道是否有人能告诉我哪里出错了?这是我的时间选择器类(timepicker2.lua):
module(..., package.seeall)
-- include Corona's "widget" library
local widget = require "widget"
--decorator--------------------
function decorate(obj, hr, min) --object to decorate
print("--init new timepicker--")
theHour = 0
theMin = 0
am = true
function incHr(event)
print("inc")
if theHour < 12 then
theHour = theHour + 1
else
theHour = 1
end
if theHour < 10 then
hrText.text = "0"..theHour
else
hrText.text = theHour
end
end
increaseHrBtn = widget.newButton{
default="gfx/up_regular.png",
over="gfx/up_press.png",
width=40, height=40,
onRelease = incHr
}
hrText = display.newText("0", 10, 41, native.systemFont, 24)
hrText:setTextColor(0, 0, 0)
local decHr = function (event)
print("inc")
if theHour > 1 then
theHour = theHour - 1
else
theHour = 12
end
if theHour < 10 then
hrText.text = "0"..theHour
else
hrText.text = theHour
end
end
decreaseHrBtn = widget.newButton{
default="gfx/down_regular.png",
over="gfx/down_press.png",
width=40, height=40,
onRelease = decHr
}
decreaseHrBtn.y = 100
dotsText = display.newText(":", 55, 39, native.systemFont, 24)
dotsText:setTextColor(0, 0, 0)
local incMin = function (event)
print("inc")
if theMin < 59 then
theMin = theMin + 1
else
theMin = 0
end
if theMin < 10 then
minText.text = "0"..theMin
else
minText.text = theMin
end
end
increaseMinBtn = widget.newButton{
default="gfx/up_regular.png",
over="gfx/up_press.png",
width=40, height=40,
onRelease = incMin
}
increaseMinBtn.x = 100
minText = display.newText("0", 90, 41, native.systemFont, 24)
minText:setTextColor(0, 0, 0)
local decMin = function (event)
print("dec")
if theMin > 0 then
theMin = theMin - 1
else
theMin = 59
end
if theMin < 10 then
minText.text = "0"..theMin
else
minText.text = theMin
end
end
decreaseMinBtn = widget.newButton{
default="gfx/down_regular.png",
over="gfx/down_press.png",
width=40, height=40,
onRelease = decMin
}
decreaseMinBtn.y = 100
decreaseMinBtn.x = 100
local toggleAmPm = function (event)
if am == true then
am = false
ampmBtn:setLabel( "PM" )
else
am = true
ampmBtn:setLabel( "AM" )
end
end
ampmBtn = widget.newButton{
default="gfx/blank_normal.png",
over="gfx/blank_press.png",
label = "AM",
font = "Korean Calligraphy",
fontSize = 25,
width=58, height=58,
onRelease = toggleAmPm
}
ampmBtn.x = 160
ampmBtn.y = 58
if hr > 12 then
if #hr < 2 then
hrText.text = "0"..hr
else
hrText.text = hr
end
theHour = hr-12
am = false
ampmBtn:setLabel( "PM" )
else
if hr < 10 then
hrText.text = "0"..hr
else
hrText.text = hr
end
theHour = hr
if hr == 12 then
am = false
ampmBtn:setLabel( "PM" )
else
am = true
ampmBtn:setLabel( "AM" )
end
end
if min < 10 then
minText.text = "0"..min
else
minText.text = min
end
theMin = min
obj:insert(increaseHrBtn.view)
obj:insert(decreaseHrBtn.view)
obj:insert(increaseMinBtn.view)
obj:insert(decreaseMinBtn.view)
obj:insert(hrText)
obj:insert(dotsText)
obj:insert(minText)
obj:insert(ampmBtn.view)
end
这是我使用它的地方:
local reminder1Group = display.newGroup()
TimePicker.decorate(reminder1Group, 0, 50)
scrollView:insert(reminder1Group)
我有一种感觉,我需要将函数与 obj 联系起来,但我正在为如何做到这一点而苦苦挣扎。希望有人能指出我正确的方向吗?非常感谢!
编辑!
到目前为止修改(和简化)的类:
module(..., package.seeall)
local widget = require "widget"
picker = {}
picker.__index = picker
function picker.new()
local picker_object = {}
setmetatable(picker_object,picker)
pickerGroup = display.newGroup()
picker_object.theHour = 0
picker_object.theMin = 0
picker_object.am = true
picker_object.increaseHrBtn = widget.newButton{
default="gfx/up_regular.png",
over="gfx/up_press.png",
width=40, height=40,
onRelease = picker.incHr
}
picker_object.decreaseHrBtn = widget.newButton{
default="gfx/down_regular.png",
over="gfx/down_press.png",
width=40, height=40,
onRelease = picker.decHr
}
picker_object.decreaseHrBtn.y = 100
pickerGroup:insert(picker_object.increaseHrBtn.view)
pickerGroup:insert(picker_object.decreaseHrBtn.view)
return picker_object
end
function picker:incHr(event)
print("inc")
end
function picker:decHr(event)
print("dec")
end
我称之为:
local TimePicker = require("timepicker2")
local reminderPicker1 = TimePicker.picker.new()
...现在想知道如何将这个时间选择器的物理方面放入滚动视图中!