コロナにOOPを適用することについて少し精神的な障害があります。アプリケーションで何度も再利用したいタイムピッカーのクラスを作成しました。同じストーリーボードシーンに2つのインスタンスを配置しようとしましたが、最初のピッカーのボタンでデータを制御し、2番目のピッカーに表示します。だから彼らはデータや機能などを共有しています。誰かが私がどこで間違っているのか教えてくれるでしょうか?これが私の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に関連付ける必要があると感じていますが、それを正確に行う方法に苦労しています。うまくいけば、誰かが私を正しい方向に向けることができますか?どうもありがとう!
編集!
これまでに変更された(および簡略化された)クラス:
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()
...そして今、このタイムピッカーの物理的な側面をスクロールビューに取り込む方法を考えています!