我有關於申請OOP電暈有點心理障礙的。我已經創建了一個時間選擇器,我希望在應用程序中重複使用多次。我試着將它的兩個實例放在同一個故事板場景中,但第一個拾取器上的按鈕控制數據並在第二個上顯示。所以他們分享數據,功能等等。我想知道有沒有人能告訴我我要去哪裏?這是我的timepicker類(timepicker2.lua):電暈OOP多個實例共享屬性
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()
...現在想知道如何這個timepicker的物理方面進入一個滾動型!
對不起,我剛剛開始接觸電暈,並用於ActionScript 3的使用其內置的OOP,所以有麻煩我圍着它轉。感謝您的示例,我會嘗試將代碼應用到我自己的代碼中。 –
再次感謝您的代碼。我已經開始重建我的課程,似乎進展順利。還有一個問題是如何從我的課程中返回一組物理按鈕等,以便我可以將它們添加到我的UI中的滾動視圖中。謝謝! –
如果您想要與每個對象關聯的按鈕,只需在新功能中創建一個按鈕即可。並添加animal_object.button =按鈕。 您可以稍後在成員函數中以self.button的身份訪問它! – SatheeshJM