So here’s the deal. I am trying to run a function inside of an event listener as such:
local addBtnStart = function () btnStartGame = display.newText( "Start Game", 0, 0, "Helvetica", 60 ) btnStartGame.x = display.contentCenterX btnStartGame.y = display.contentCenterY-200 btnStartGame.alpha=0 transition.to( btnStartGame, {time=1000, alpha=1, y=display.contentCenterY, x=display.contentCenterX } ) btnStartGame:addEventListener ( "tap", woof() ) end
Well, woof() causes errors. As a global function waaay up top I have…
local function woof() audio.play( sndWoof ) end
HOWEVER, if I change the event listener to…
btnStartGame:addEventListener ( "tap", woof )
AND change the function woof into a function inside a variable as such…
local woof = function() audio.play( sndWoof ) end
It works! What have I stumbled upon? What is the difference between a function and a function inside a variable? Why will a function inside a variable work in an event listener, but an actual function will NOT work in such an instance? The error I get when trying to run a function is attached as an image.
I’m new to the concept of a function inside of a variable, but it seems I can add say…
local woof = function( var1 )
or multiple in-values. What’s the difference between a function inside of a variable and an actual function? Why are they treated differently in my code (why does one work and the other doesn’t)?
Thanks!
-Chris