stack traceback error in corona

hello there i am programing a mobile game with corona sdk using the lua language. i encountered a problem while adding and removing event listeners, it is going as follows:

i have an object which when you “touch” it, it’s alpha goes down to 0.5, and this removes the event listener that causes this, and now a new event listener is applied which calls another function to get it’s alpha back to 1, this time it will return the previous function event listener to enable it to keep making a loop. the functions are applied as follow:

object:addEventListener(“touch”, dropAlpha);

local function dropAlpha(event)
event.target.alpha = 1;
object:addEventListener(“touch”, dropAlpha);
object:removeEventListener(“touch”, increaseAlpha);
end

local function dropAlpha(event)
event.target.alpha = 0.5;
object:removeEventListener(“touch”, dropAlpha);
object:addEventListener(“touch”, increaseAlpha);
end

the problem is as you can see is the stack traceback is wrong, because that does not let it read a function that is not defined already in the code. what can i do inorder to make it work, without using 1 function that asks: ‘IF(alpha = 1) do this ELSE do this END.’ thanks in advance.

The easiest way around this would be to use a forward declaration:

--declare the functions, right at the top of your code local dropAlpha local increaseAlpha --define functions anywhere AFTER declaring them increaseAlpha = function(event) event.target.alpha = 1; object:addEventListener("touch", dropAlpha); object:removeEventListener("touch", increaseAlpha); end dropAlpha = function(event) event.target.alpha = 0.5; object:removeEventListener("touch", dropAlpha); object:addEventListener("touch", increaseAlpha); end 

Although you said you don’t want to use 1 function with an else statement (I’m sure you have your reasons), doing this seems much neater to me:

local function swapAlpha(event) if event.target.alpha == 0.5 then event.target.alpha = 1.0 else event.target.alpha = 0.5 end end

The easiest way around this would be to use a forward declaration:

--declare the functions, right at the top of your code local dropAlpha local increaseAlpha --define functions anywhere AFTER declaring them increaseAlpha = function(event) event.target.alpha = 1; object:addEventListener("touch", dropAlpha); object:removeEventListener("touch", increaseAlpha); end dropAlpha = function(event) event.target.alpha = 0.5; object:removeEventListener("touch", dropAlpha); object:addEventListener("touch", increaseAlpha); end 

Although you said you don’t want to use 1 function with an else statement (I’m sure you have your reasons), doing this seems much neater to me:

local function swapAlpha(event) if event.target.alpha == 0.5 then event.target.alpha = 1.0 else event.target.alpha = 0.5 end end