我对将OOP应用于Corona有一点心理障碍。我已经创建了一个类,它是一个时间戳,我希望在我的应用程序中多次重复使用。我尝试在同一个故事板场景中放置它的两个实例,但第一个选择器上的按钮控制数据并显示在第二个。所以他们分享数据,功能等等。我想知道是否有人能告诉我哪里出错了?这是我的timepicker类(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,但我正在努力解决这个问题。希望有人可以指出我正确的方向? 非常感谢!
EDIT!
到目前为止修改(和简化)类:
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()
...现在想知道如何将这个时间戳的物理方面变成滚动视图!
答案 0 :(得分:1)
你还没有在这里使用OOP!在所有! 你在这里做的是你已经声明了一个函数(虽然是在一个单独的模块中)并调用它。
在这里阅读http://lua-users.org/wiki/SimpleLuaClasses 在这里http://lua-users.org/wiki/ObjectOrientationTutorial
以下是你如何在lua(以及Corona)中使用OOP的要点
local animals = {}
animals.__index = animals
function animals.new(params)
local animal_object = {}
setmetatable(animal_object,animals)
animal_object.name = params.name
return animal_object
end
function animals:getName()
return self.name
end
以下是您使用该课程的方式
local dog = animals.new{name="dog"}
local cat = animals.new{name="cat"}
local name = dog:getName()
print(name) -- prints "dog"
local name = cat:getName()
print(name) -- prints "cat
你需要注意的是,当你调用函数cat:getName而不是cat.getName(使用分号而不是dot)时,你将'self'作为参数传递。
那个自我是“动物”这个阶级的“对象”
答案 1 :(得分:0)
我不知道Corona的最小问题,更不用说它是OOP系统了,但是你在函数中使用全局变量的事实引起了怀疑...
theHour = 0
theMin = 0
am = true
这些都是全局变量,Lua使用全局默认范例。因此,除非您在它们之前添加local
关键字,否则它们将由您的整个应用程序共享。