adding functions inside a scene scope

I’m having an issue with adding functions that are inside a scene’s scope. This is more of a “new to lua” issue

I’ve got a button factory, that created a button that has an on release function 

when I converted the function from 

function onButton(event)

to

function scene:onButton(event)

the callback no longer works (does nothing)

onRelease = onButton

nor does (causes error)

onRelease = scene:onButton

I also tried using a closure,

onRelease = function(event) scene:onButton(event) end

any idea how I can pass this function?

Have you looked at widgets? 

http://docs.coronalabs.com/api/library/widget/newButton.html

Widget buttons look something like this:

local widget = require("widget") local function onBtnRelease(event) local target = event.target if target.myId == "Play" then -- do something here to handle the Play Button elseif target.myId == "Home" then -- do something here to handle the Home Button elseif target.myId == "Rate" then -- do something here to handle the Rate Button elseif target.myId == "Exit" then -- do something here to handle the Exit Button end return true -- indicates successful touch end local myButtons = {} myButtons[1] = widget.newButton{ labelColor = { default={0}, over={128} }, defaultFile="playbutton1.png", overFile="playbutton2.png", width=125, height=125, onRelease = onBtnRelease } myButtons[1].x = display.contentWidth \*.2 myButtons[1].y = display.contentHeight \*.7 myButtons[1].myId = "Play" myButtons[2] = widget.newButton{ labelColor = { default={0}, over={128} }, defaultFile="home.png", overFile="home2.png", width=125, height=125, onRelease = onBtnRelease } myButtons[2].x = display.contentWidth \*.4 myButtons[2].y = display.contentHeight \*.7 myButtons[2].myId = "Home" myButtons[3] = widget.newButton{ labelColor = { default={0}, over={128} }, defaultFile="rate.png", overFile="rate2.png", width = 125, height = 125, onRelease = onBtnRelease } myButtons[3].x = display.contentWidth \*.6 myButtons[3].y = display.contentHeight \*.7 myButtons[3].myId = "Rate" myButtons[4] = widget.newButton{ labelColor = { default={0}, over={128} }, defaultFile="exit1.png", overFile="exit2.png", width = 125, height = 125, onRelease = onBtnRelease } myButtons[4].x = display.contentWidth \*.8 myButtons[4].y = display.contentHeight \*.7 myButtons[4].myId = "Exit"

I prefer doing 

addButton("BTN 1") addButton("BTN 2") addButton("BTN 3") addButton("BTN 4")

rather than repeating 90% of code, it comes in especially handy if I have a variable amount of buttons.

Also this closure did work 

onRelease = function(event) scene:onButton(event) end 

I’m trying to figure out why now

I’m not 100% sure what you want to achieve. But technically your last example should work.

function scene:onButton(event) . . . onRelease = function(event) scene:onButton(event) end

The reason the other approaches don’t work is because when you declare a function like scene:myFunction you are implying that the first argument will be a reference to an instance.

scene:myFunction(event) is just a shorthand way of writing scene.myFunction(self, event). (notice : vs .)

When you give a reference to a function you can only use the “.” notation. The “:” notation is only used for calling.

I assume you’re using the onRelease parameter of a widget button? This parameter knows nothing about an instance, so your first attempts would not pass ‘self’. The closure will call the function with a reference to its scene instance.

Have you looked at widgets? 

http://docs.coronalabs.com/api/library/widget/newButton.html

Widget buttons look something like this:

local widget = require("widget") local function onBtnRelease(event) local target = event.target if target.myId == "Play" then -- do something here to handle the Play Button elseif target.myId == "Home" then -- do something here to handle the Home Button elseif target.myId == "Rate" then -- do something here to handle the Rate Button elseif target.myId == "Exit" then -- do something here to handle the Exit Button end return true -- indicates successful touch end local myButtons = {} myButtons[1] = widget.newButton{ labelColor = { default={0}, over={128} }, defaultFile="playbutton1.png", overFile="playbutton2.png", width=125, height=125, onRelease = onBtnRelease } myButtons[1].x = display.contentWidth \*.2 myButtons[1].y = display.contentHeight \*.7 myButtons[1].myId = "Play" myButtons[2] = widget.newButton{ labelColor = { default={0}, over={128} }, defaultFile="home.png", overFile="home2.png", width=125, height=125, onRelease = onBtnRelease } myButtons[2].x = display.contentWidth \*.4 myButtons[2].y = display.contentHeight \*.7 myButtons[2].myId = "Home" myButtons[3] = widget.newButton{ labelColor = { default={0}, over={128} }, defaultFile="rate.png", overFile="rate2.png", width = 125, height = 125, onRelease = onBtnRelease } myButtons[3].x = display.contentWidth \*.6 myButtons[3].y = display.contentHeight \*.7 myButtons[3].myId = "Rate" myButtons[4] = widget.newButton{ labelColor = { default={0}, over={128} }, defaultFile="exit1.png", overFile="exit2.png", width = 125, height = 125, onRelease = onBtnRelease } myButtons[4].x = display.contentWidth \*.8 myButtons[4].y = display.contentHeight \*.7 myButtons[4].myId = "Exit"

I prefer doing 

addButton("BTN 1") addButton("BTN 2") addButton("BTN 3") addButton("BTN 4")

rather than repeating 90% of code, it comes in especially handy if I have a variable amount of buttons.

Also this closure did work 

onRelease = function(event) scene:onButton(event) end 

I’m trying to figure out why now

I’m not 100% sure what you want to achieve. But technically your last example should work.

function scene:onButton(event) . . . onRelease = function(event) scene:onButton(event) end

The reason the other approaches don’t work is because when you declare a function like scene:myFunction you are implying that the first argument will be a reference to an instance.

scene:myFunction(event) is just a shorthand way of writing scene.myFunction(self, event). (notice : vs .)

When you give a reference to a function you can only use the “.” notation. The “:” notation is only used for calling.

I assume you’re using the onRelease parameter of a widget button? This parameter knows nothing about an instance, so your first attempts would not pass ‘self’. The closure will call the function with a reference to its scene instance.