Note that there are some explanatory texts on larger screens.

plurals
  1. POReturning a display from a Corona OOP class
    text
    copied!<p>I am new to Corona and am trying to get some basic OOP going on. I have an application which requires a number of time picker widgets, and would like to be able to reuse a 'timepicker' class for each instance. Here's my code so far (bare bones - timepicker.lua):</p> <pre><code>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 </code></pre> <p>and here is where I instantiate it:</p> <pre><code>local TimePicker = require("timepicker") local reminderPicker1 = TimePicker.picker.new() </code></pre> <p>This gives me a functioning time picker on the screen (or the first two buttons of which, anyhow). But I want to grab the display objects of this picker and put them into my scroll view on the screen. Because the class returns an object, this can't be inserted into the scroll view. I'm sure it's a basic thing, but I can't figure out what I need to do next! Can anyone help? Much appreciated.</p> <p>EDIT</p> <p>Posting my class, which seems to be almost there, except am having trouble accessing the variables 'theHour' and 'theMin' from within the class...</p> <pre><code>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 &lt; 12 then self.theHour = self.theHour + 1 else self.theHour = 1 end if self.theHour &lt; 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 &gt; 1 then self.theHour = self.theHour - 1 else self.theHour = 12 end if self.theHour &lt; 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 &lt; 59 then self.theMin = self.theMin + 1 else self.theMin = 0 end if self.theMin &lt; 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 &gt; 0 then self.theMin = self.theMin - 1 else self.theMin = 59 end if self.theMin &lt; 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 </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload