Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

Call function method (example 2 Frame button) to avoid rewriting code causing attempt to index local 'button' (a boolean value)
Started by KAOOS Mar 03 2019 12:03 PM

3 replies to this topic
simplification best practice parameter function parameters function module method button beginner learn

Best Answer nick_sherman , 04 March 2019 - 12:40 AM

Not tested as not on a machine with Corona installed, but you should get the idea.

 

newButton2Frame.lua

 

 
local m = {}
 
local widget = require( "widget" )
 
local options = {
      width = 240,
      height = 120,
      numFrames = 2,
      sheetContentWidth = 480,
      sheetContentHeight = 120
  }
  
local buttonSheet = graphics.newImageSheet( "widget-button-file.png", options )
 
m.new = function (xValue, yValue, label, id, listener)
 
  local button = widget.newButton(
      {
          sheet = buttonSheet,
          defaultFrame = 1,
          overFrame = 2,
          label = "button",
          onEvent = listener
      }
  )
  
  -- Center the button
  button.x = xValue
  button.y = yValue
  
  -- Change the button's label text
  button:setLabel(label) -- buttonName ) --"2-Frame" )
  button.id = id
  
  return button
 
 
end
 
 
return m
 
 
 
Usage:
 
 
 
 
local newButton2Frame = require("newButton2Frame")   -- load module
 
local function handleButtonEvent( event )
    
        if ( "ended" == event.phase ) then
            print( event.target.id.." button was pressed and released" )
        end
  
        -- PUT LISTENER IN SCENE SO BUTTON ACTIONS CAN INTERACT WITH IT
end
 
local button1 = newButton2Frame.new(400,600, "My First Button", "Play", handleButtonEvent)
local button2 = newButton2Frame.new(500,600, "My Second Button", "Settings", handleButtonEvent)
 

[TOPIC CONTROLS]
This topic has been archived. This means that you cannot reply to this topic.
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

KAOOS

[GLOBAL: userInfoPane.html]
KAOOS
  • Observer

  • 2 posts
  • Corona SDK

Hi all

 

I simply want to make the "2 Frame Button" from https://docs.coronalabs.com/api/library/widget/newButton.html reusable in my code. The problem I face is that the x-position, y-position and name are predefined in the original code. If i have 3 buttons they will all get the same x & y-position and the same name. I tried to create a function  functionInputs to add parameters to the function but this did not work. Any help would be highly appreciated.

 

Thank you!

 

 

 

-- This example works but position and name is from "newButton2Frame.lua"
local newButton2Frame = require("newButton2Frame") -- Works

local button1 = newButton2Frame -- Works

-- To give parameters for x-Position (600), y-Position (400) and name of button (button 1) does not work
-- With this working i could easily create other buttons to not rewrite the code
local button2 = newButton2Frame.functionInputs(600, 400, "button 1") -- Does not work

 

newButton2Frame.lua

local widget = require( "widget" )
--
--local M = {} --> Did not work

-- Tried to add function to use parameters for x, y & name
local function functionInputs(xValue, yValue, buttonName)

    -- Function to handle button events
    local function handleButtonEvent( event )
    
        if ( "ended" == event.phase ) then
            print( "Button was pressed and released" )
        end
    end
    
    -- Image sheet options and declaration
    -- For testing, you may copy/save the image under "2-Frame Construction" above
    local options = {
        width = 240,
        height = 120,
        numFrames = 2,
        sheetContentWidth = 480,
        sheetContentHeight = 120
    }
    
    local buttonSheet = graphics.newImageSheet( "widget-button-file.png", options )
    
    -- Create the widget
    local button1 = widget.newButton(
        {
            sheet = buttonSheet,
            defaultFrame = 1,
            overFrame = 2,
            label = "button",
            onEvent = handleButtonEvent
        }
    )
    
    -- Center the button
    button1.x = xValue -- display.contentCenterX
    button1.y = yValue --display.contentCenterY
    
    -- Change the button's label text
    button1:setLabel( "blubb") -- buttonName ) --"2-Frame" )


end

--return M

 

 

original code - https://docs.coronalabs.com/api/library/widget/newButton.html

-- 2-Frame

local widget = require( "widget" )
 
-- Function to handle button events
local function handleButtonEvent( event )
 
    if ( "ended" == event.phase ) then
        print( "Button was pressed and released" )
    end
end
 
-- Image sheet options and declaration
-- For testing, you may copy/save the image under "2-Frame Construction" above
local options = {
    width = 240,
    height = 120,
    numFrames = 2,
    sheetContentWidth = 480,
    sheetContentHeight = 120
}
local buttonSheet = graphics.newImageSheet( "widget-button-file.png", options )
 
-- Create the widget
local button1 = widget.newButton(
    {
        sheet = buttonSheet,
        defaultFrame = 1,
        overFrame = 2,
        label = "button",
        onEvent = handleButtonEvent
    }
)
 
-- Center the button
button1.x = display.contentCenterX
button1.y = display.contentCenterY
 
-- Change the button's label text
button1:setLabel( "2-Frame" )


[TOPIC: post.html]
#2

nick_sherman

[GLOBAL: userInfoPane.html]
nick_sherman
  • Corona Geek

  • 1,820 posts
  • Corona SDK

  Best Answer

Not tested as not on a machine with Corona installed, but you should get the idea.

 

newButton2Frame.lua

 

 
local m = {}
 
local widget = require( "widget" )
 
local options = {
      width = 240,
      height = 120,
      numFrames = 2,
      sheetContentWidth = 480,
      sheetContentHeight = 120
  }
  
local buttonSheet = graphics.newImageSheet( "widget-button-file.png", options )
 
m.new = function (xValue, yValue, label, id, listener)
 
  local button = widget.newButton(
      {
          sheet = buttonSheet,
          defaultFrame = 1,
          overFrame = 2,
          label = "button",
          onEvent = listener
      }
  )
  
  -- Center the button
  button.x = xValue
  button.y = yValue
  
  -- Change the button's label text
  button:setLabel(label) -- buttonName ) --"2-Frame" )
  button.id = id
  
  return button
 
 
end
 
 
return m
 
 
 
Usage:
 
 
 
 
local newButton2Frame = require("newButton2Frame")   -- load module
 
local function handleButtonEvent( event )
    
        if ( "ended" == event.phase ) then
            print( event.target.id.." button was pressed and released" )
        end
  
        -- PUT LISTENER IN SCENE SO BUTTON ACTIONS CAN INTERACT WITH IT
end
 
local button1 = newButton2Frame.new(400,600, "My First Button", "Play", handleButtonEvent)
local button2 = newButton2Frame.new(500,600, "My Second Button", "Settings", handleButtonEvent)
 

Edited by nick_sherman, 04 March 2019 - 12:42 AM.


[TOPIC: post.html]
#3

KAOOS

[GLOBAL: userInfoPane.html]
KAOOS
  • Observer

  • 2 posts
  • Corona SDK

hello Nick

 

THANK YOU SO MUCH!

It works now!

 

The only thing i changed to your input was leaving this code inside of newButton2Frame.lua

    -- Function to handle button events
    local function handleButtonEvent( event )
    
        if ( "ended" == event.phase ) then
            print( "Button was pressed and released" )
        end
    end

 

And now i can call

local newButton2Frame = require("newButton2Frame")

 

 

local button1 = newButton2Frame.new(250, 350, "button 1")

local button2 = newButton2Frame.new(250, 500, "button 2")

 

This is exactly was i needed! :D

 

Thanks again Nick!



[TOPIC: post.html]
#4

nick_sherman

[GLOBAL: userInfoPane.html]
nick_sherman
  • Corona Geek

  • 1,820 posts
  • Corona SDK

No problem. Only issue I can see with having the handler inside of newButton2Frame.lua is if you need to call code outside of that module. 




[topic_controls]
 
[/topic_controls]