Reputation: 861
I'm having a bit of a mental block about applying OOP to Corona. I've made a class which is a timepicker that I wish to re-use multiple times in my application. I tried putting two instances of it in the same storyboard scene, but the buttons on the first picker control the data and display on the second. So they are sharing data, functions, etc. I wonder if anyone can tell me where I'm going wrong? Here's my timepicker class (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
and here's where I use it:
local reminder1Group = display.newGroup()
TimePicker.decorate(reminder1Group, 0, 50)
scrollView:insert(reminder1Group)
I have a feeling I need to tie the functions to the obj but am struggling with how exactly to do that. Hopefully someone can point me in the right direction? Many thanks!
EDIT!
Modified (and simplified) class so far:
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
and where I call it:
local TimePicker = require("timepicker2")
local reminderPicker1 = TimePicker.picker.new()
... and now wondering how to get the physical aspects of this timepicker into a scrollview!
Upvotes: 0
Views: 527
Reputation: 3643
You haven't used OOP here! AT ALL! What you have done here is you have declared a function (albeit in a separate module) and called it.
Have a read here http://lua-users.org/wiki/SimpleLuaClasses and here http://lua-users.org/wiki/ObjectOrientationTutorial
Here's a gist of how you use OOP in lua(and hence Corona)
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
and here's how you use the class
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
The thing you have to note is that when you call a function cat:getName as opposed to cat.getName (using semicolon instead of dot) you pass 'self' as an argument.
That self is the "object" of the class "animals"
'
Upvotes: 1
Reputation: 9549
I don't know the least thing about Corona, let alone it's OOP system, but the fact that you use globals in your function raises suspicion...
theHour = 0
theMin = 0
am = true
These are all globals, Lua uses the global per default paradigm. So unless you put a local
keyword before them, they will be shared by your whole application.
Upvotes: 0