Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8000739
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T15:53:46+00:00 2026-06-04T15:53:46+00:00

I am new to Corona and am trying to get some basic OOP going

  • 0

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):

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 here is where I instantiate it:

local TimePicker = require("timepicker")
local reminderPicker1 = TimePicker.picker.new()

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.

EDIT

Posting my class, which seems to be almost there, except am having trouble accessing the variables ‘theHour’ and ‘theMin’ from within the class…

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T15:53:48+00:00Added an answer on June 4, 2026 at 3:53 pm

    Ah! you had asked in your previous question right. I forgot to answer that.

    Anyway you can access the display objects as
    reminderPicker1.increaseHrBtn,reminderPicker1.decreaseHrBtn and so on.

    Inside class methods you can access them as
    self.increaseHrBtn, self.decreaseHrBtn

    EDIT

    Change onRelease = picker.toggleAmPm to

    onRelease = function() picker_object:toggleAmPm() end 
    

    And change picker_object.ampmBtn:setLabel( "PM" )to

    self.ampmBtn:setLabel( "PM" )
    

    DO the same for all other instances. And you’ll be fine!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to Corona SDK and Lua language and I have some troubles.. so
New to rails and trying to get a one to many relationship up and
New to Haskell and have a stumbling block. I'm trying to filter a list
New to DDD here and have a architecture question which should be a typical
I am new to corona and my Problem id I have a long scene
New to linux and trying to escape doing this the hard way. I have
New Android programmer here. I have a Service which performs socket management and async
I'm learning Corona SDK and am new to lua as well (i mainly do
New Question I am looking for a way in Javascript to get the parent
new_story GET /story/new(.:format) {:action=>new, :controller=>stories} edit_story GET /story/edit(.:format) {:action=>edit, :controller=>stories} story GET /story(.:format) {:action=>show,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.