我是 Corona 的新手,正在尝试进行一些基本的 OOP。我有一个需要多个时间选择器小部件的应用程序,并且希望能够为每个实例重用一个“时间选择器”类。到目前为止,这是我的代码(简单的 - timepicker.lua):
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("timepicker")
local reminderPicker1 = TimePicker.picker.new()
这给了我一个在屏幕上正常工作的时间选择器(或者其中的前两个按钮,无论如何)。但我想抓住这个选择器的显示对象并将它们放入屏幕上的滚动视图中。因为该类返回一个对象,所以不能将其插入到滚动视图中。我确定这是一件基本的事情,但我不知道接下来我需要做什么!任何人都可以帮忙吗?非常感激。
编辑
发布我的课程,这似乎几乎就在那里,除了在课堂内访问变量“theHour”和“theMin”时遇到问题......
module(..., package.seeall)
local widget = require "widget"
picker = {}
picker.__index = picker
function picker.new()
local picker_object = {}
setmetatable(picker_object,picker)
picker_object.theHour = 12
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
picker_object.hrText = display.newText("12", 6, 41, native.systemFont, 24)
picker_object.hrText:setTextColor(0, 0, 0)
picker_object.dotsText = display.newText(":", 58, 39, native.systemFont, 24)
picker_object.dotsText:setTextColor(0, 0, 0)
picker_object.increaseMinBtn = widget.newButton{
default="gfx/up_regular.png",
over="gfx/up_press.png",
width=40, height=40,
onRelease = picker.incMin
}
picker_object.increaseMinBtn.x = 100
picker_object.decreaseMinBtn = widget.newButton{
default="gfx/down_regular.png",
over="gfx/down_press.png",
width=40, height=40,
onRelease = picker.decMin
}
picker_object.decreaseMinBtn.y = 100
picker_object.decreaseMinBtn.x = 100
picker_object.minText = display.newText("00", 86, 41, native.systemFont, 24)
picker_object.minText:setTextColor(0, 0, 0)
picker_object.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 = picker.toggleAmPm
}
picker_object.ampmBtn.x = 160
picker_object.ampmBtn.y = 58
return picker_object
end
function picker:getHour()
return self.theHour
end
function picker:getMin()
return self.theMin
end
function picker:incHr(event)
print("inc")
if self.theHour < 12 then
self.theHour = self.theHour + 1
else
self.theHour = 1
end
if self.theHour < 10 then
picker_object.hrText.text = "0"..self.theHour
else
picker_object.hrText.text = self.theHour
end
end
function picker:decHr(event)
print("dec")
if self.theHour > 1 then
self.theHour = self.theHour - 1
else
self.theHour = 12
end
if self.theHour < 10 then
picker_object.hrText.text = "0"..self.theHour
else
picker_object.hrText.text = self.theHour
end
end
function picker:incMin(event)
print("inc")
if self.theMin < 59 then
self.theMin = self.theMin + 1
else
self.theMin = 0
end
if self.theMin < 10 then
picker_object.minText.text = "0"..self.theMin
else
self.minText.text = self.theMin
end
end
function picker:decMin(event)
print("dec")
if self.theMin > 0 then
self.theMin = self.theMin - 1
else
self.theMin = 59
end
if self.theMin < 10 then
picker_object.minText.text = "0"..self.theMin
else
picker_object.minText.text = self.theMin
end
end
function picker:toggleAmPm(event)
if self.am == true then
self.am = false
picker_object.ampmBtn:setLabel( "PM" )
else
self.am = true
picker_object.ampmBtn:setLabel( "AM" )
end
end