Handling Events in separate class

I want to call a function from a separate class or module when the button is touched. This code works fine when both the function and eventlistener are in the same file.

local function startGame(e)  
 if(e.phase == "ended") then  
 print "Game started";  
 end  
end  
  
button1:addEventListener("touch", startGame);  

What I want to do is this…

button1:addEventListener("touch", GameManager:startGame());  

GameManager would have the function startGame. What is the syntax for calling GameManager:startGame() when it is inside an event listener because usually the function wouldnt have the parenthesis
[import]uid: 39238 topic_id: 7078 reply_id: 307078[/import]

Is GameManager a table or a module? I’ve seen people attempt to use the colon when calling functions from a module and that is incorrect. The colon is only for calling methods (ie. functions owned by a table,) use a period for calling top-level functions.

Also, you declared startGame() as a local function. The entire point of “local” is that it only applies within the current scope, so if you want to use a function in other modules it cannot be local. [import]uid: 12108 topic_id: 7078 reply_id: 24915[/import]

not sure actually but you could always do

[lua]button1:addEventListener(“touch”, function() GameManager:startGame() end);[/lua]

but really you want to do it like this:

[lua]local function onTap(event)
GameManager:startGame()
end

button1:addEventListener(“tap”, onTap)[/lua] [import]uid: 6645 topic_id: 7078 reply_id: 24914[/import]

Thanks Jmp. I might do it like that. I just wanted to to try and keep the touch event functions outstide of the class to make the code more organized.

GameManager looks like this…

module(..., package.seeall)  
require("GamePlay");  
  
local currentLevelNumber;  
local currentScore;  
local currentObject;  
local readyToPlay = false;  
  
function GameManager:setReadyToPlay(condition)  
 readyToPlay = condition;  
end  
  
function GameManager:getReadyToPlay()  
 return readyToPlay;  
end  
  
function GameManager:setCurrentLevelNumber(levelNumber)  
 currentLevelNumber = levelNumber;  
end  
  
function GameManager:getCurrentLevelNumber()  
 return currentLevelNumber;  
end  

Right now I would call these functions like GameManager:getCurrentLevelNumber();
Is that the right way to call it? It works, but I want to make sure I’m doing it the best way. Also, I know how local functions work, I just didn’t show the entirety of the 2nd part of that code. [import]uid: 39238 topic_id: 7078 reply_id: 24993[/import]