0

我对将 OOP 应用于 Corona 有一点心理障碍。我创建了一个时间选择器类,我希望在我的应用程序中多次重复使用它。我尝试将它的两个实例放在同一个故事板场景中,但第一个选择器上的按钮控制数据并在第二个选择器上显示。所以他们正在共享数据、功能等。我想知道是否有人能告诉我哪里出错了?这是我的时间选择器类(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()

...现在想知道如何将这个时间选择器的物理方面放入滚动视图中!

4

2 回答 2

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(使用分号而不是点)时,您将“self”作为参数传递。
那个自我是“动物”类的“对象”

'

于 2012-05-27T09:05:33.220 回答
0

我对Corona一无所知,更不用说它是OOP系统了,但是您在函数中使用全局变量这一事实引起了怀疑……

theHour = 0
theMin = 0
am = true

这些都是全局变量,Lua 使用默认的全局范式。因此,除非您在它们之前放置local关键字,否则它们将由您的整个应用程序共享。

于 2012-05-27T08:38:02.513 回答