从Corona OOP类返回显示

时间:2012-05-29 15:14:24

标签: lua corona

我是Corona的新手,我正试图让一些基本的OOP继续下去。我有一个需要大量时间选择器小部件的应用程序,并且希望能够为每个实例重用“timepicker”类。这是我到目前为止的代码(bare bones - timepicker.lua):

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("timepicker")
local reminderPicker1 = TimePicker.picker.new()

这给了我一个在屏幕上运行的时间选择器(或者无论如何都是前两个按钮)。但我想抓住这个选择器的显示对象并将它们放入屏幕上的滚动视图中。因为类返回一个对象,所以无法将其插入到滚动视图中。我确定这是一个基本的事情,但我无法弄清楚接下来我需要做什么!有人可以帮忙吗?非常感谢。

修改

发布我的课程,似乎几乎就在那里,除了我在课堂上访问变量'theHour'和'theMin'时遇到了麻烦......

module(..., package.seeall)

local widget = require "widget"

picker = {}
picker.__index = picker

function picker.new()
    local picker_object = {}
    setmetatable(picker_object,picker)

    picker_object.theHour = 12
    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

    picker_object.hrText = display.newText("12", 6, 41, native.systemFont, 24)
    picker_object.hrText:setTextColor(0, 0, 0)

    picker_object.dotsText = display.newText(":", 58, 39, native.systemFont, 24)
    picker_object.dotsText:setTextColor(0, 0, 0)

    picker_object.increaseMinBtn = widget.newButton{
        default="gfx/up_regular.png",
        over="gfx/up_press.png",
        width=40, height=40,
        onRelease = picker.incMin
    }

    picker_object.increaseMinBtn.x = 100

    picker_object.decreaseMinBtn = widget.newButton{
        default="gfx/down_regular.png",
        over="gfx/down_press.png",
        width=40, height=40,
        onRelease = picker.decMin
    }
    picker_object.decreaseMinBtn.y = 100
    picker_object.decreaseMinBtn.x = 100

    picker_object.minText = display.newText("00", 86, 41, native.systemFont, 24)
    picker_object.minText:setTextColor(0, 0, 0)

    picker_object.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 = picker.toggleAmPm
    }

    picker_object.ampmBtn.x = 160
    picker_object.ampmBtn.y = 58

    return picker_object
end

function picker:getHour()
    return self.theHour
end

function picker:getMin()
    return self.theMin
end

function picker:incHr(event)
    print("inc")
    if self.theHour < 12 then
        self.theHour = self.theHour + 1
    else 
        self.theHour = 1
    end

    if self.theHour < 10 then
        picker_object.hrText.text = "0"..self.theHour
    else
        picker_object.hrText.text = self.theHour
    end
end

function picker:decHr(event)
    print("dec")
    if self.theHour > 1 then
        self.theHour = self.theHour - 1
    else 
        self.theHour = 12
    end
    if self.theHour < 10 then
        picker_object.hrText.text = "0"..self.theHour
    else
        picker_object.hrText.text = self.theHour
    end
end

function picker:incMin(event)
    print("inc")
    if self.theMin < 59 then
        self.theMin = self.theMin + 1
    else 
        self.theMin = 0
    end

    if self.theMin < 10 then
        picker_object.minText.text = "0"..self.theMin
    else
        self.minText.text = self.theMin
    end
end

function picker:decMin(event)
    print("dec")
    if self.theMin > 0 then
        self.theMin = self.theMin - 1
    else 
        self.theMin = 59
    end
    if self.theMin < 10 then
        picker_object.minText.text = "0"..self.theMin
    else
        picker_object.minText.text = self.theMin
    end
end

function picker:toggleAmPm(event)
    if self.am == true then
        self.am = false
        picker_object.ampmBtn:setLabel( "PM" )
    else
        self.am = true
        picker_object.ampmBtn:setLabel( "AM" )
    end
end

1 个答案:

答案 0 :(得分:1)

啊!你在上一个问题中已经问过了。我忘了回答。

无论如何,您可以访问显示对象 reminderPicker1.increaseHrBtnreminderPicker1.decreaseHrBtn等等。

在类内部方法中,您可以将它们作为 self.increaseHrBtnself.decreaseHrBtn

修改

onRelease = picker.toggleAmPm更改为

onRelease = function() picker_object:toggleAmPm() end 

picker_object.ampmBtn:setLabel( "PM" )更改为

self.ampmBtn:setLabel( "PM" )

对所有其他实例都这样做。你会没事的!