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