Having trouble adding an event handler to a button in composer

Hi,

My code is as follows

local composer = require( "composer" ) local scene = composer.newScene() --------------------------------------------------------------------------------- -- All code outside of the listener functions will only be executed ONCE -- unless "composer.removeScene()" is called. --------------------------------------------------------------------------------- -- local forward references should go here --------------------------------------------------------------------------------- -- "scene:create()" function scene:create( event ) local sceneGroup = self.view self.\_background = display.newRect(self.view, display.contentWidth/2, display.contentHeight/2, display.contentWidth, display.contentHeight ) self.\_background.strokeWidth = 3 self.\_background:setFillColor(0, 0, 0 ) self.\_background.alpha = 0.5 self.playButton = display.newImageRect(self.view, "assets/common/play.jpg", 225, 225) self.playButton.x = display.contentWidth/2 self.playButton.y =display.contentHeight/2 self:AddEventListeners() --Add the touch listener -- Initialize the scene here. -- Example: add display objects to "sceneGroup", add touch listeners, etc. end function scene:TouchHandler() print("yay") end function scene:AddEventListeners() self.playButton = TouchHandler self.playButton:addEventListener("touch", self.playButton) end -- "scene:show()" function scene:show( event ) local sceneGroup = self.view local phase = event.phase if ( phase == "will" ) then -- Called when the scene is still off screen (but is about to come on screen). elseif ( phase == "did" ) then -- Called when the scene is now on screen. -- Insert code here to make the scene come alive. -- Example: start timers, begin animation, play audio, etc. end end -- "scene:hide()" function scene:hide( event ) local sceneGroup = self.view local phase = event.phase if ( phase == "will" ) then -- Called when the scene is on screen (but is about to go off screen). -- Insert code here to "pause" the scene. -- Example: stop timers, stop animation, stop audio, etc. elseif ( phase == "did" ) then -- Called immediately after scene goes off screen. end end -- "scene:destroy()" function scene:destroy( event ) local sceneGroup = self.view -- Called prior to the removal of scene's view ("sceneGroup"). -- Insert code here to clean up the scene. -- Example: remove display objects, save state, etc. end --------------------------------------------------------------------------------- -- Listener setup scene:addEventListener( "create", scene ) scene:addEventListener( "show", scene ) scene:addEventListener( "hide", scene ) scene:addEventListener( "destroy", scene ) --------------------------------------------------------------------------------- return scene

I am trying to add the event handler by calling

self:AddEventListeners()  

which in turn should do the following

function scene:AddEventListeners() self.playButton = TouchHandler self.playButton:addEventListener("touch", self.playButton) end

but it says self.playButton is unknown… so how do I do it?

Thanks

Shouldn’t it be:

self.playButton = self:TouchHandler
self.playButton:addEventListener(“touch”, self.playButton)

Rob,

for the second line it says, function argument expected near ‘self’

Maybe try and move these two functions

function scene:TouchHandler()
function scene:AddEventListeners()
 

before scene:create?  I’m reaching here.  I never attach those types of functions to the scene, just leaving them local to the module itself.

Rob

Rob,

Yeah moving them doesn’t do anything… I did the following

local function TouchFunction(event) scene:TouchHandler(event) end

function scene:AddEventListeners() self.playButton:addEventListener("touch", TouchFunction) end

function scene:TouchHandler(event) print("yay") end

and that worked,   just was concerned I was not doing this right… as in one of my previous posts I was told I was not attaching the events properly… 

http://forums.coronalabs.com/topic/45122-collision-event-not-firing-for-dynamic-object-with-static-object/

I almost never build my  listeners like that.  I prefer the keep it simple silly method:

local function myTouchHandler(event)

end

playButton:addEventListener(“touch”, myTouchHandler)

I personally don’t like table based listeners and prefer function based ones.  Also I understand the value of adding some objects to the scene object so they can be accessed without using upvalues, but in most cases, they don’t need accessed outside and it complicates things.

Maybe it should be:

self.playButton = self.TouchHandler
self.playButton:addEventListener(“touch”, self.playButton)

basically whatever self.playButton is is not a function which is why it’s complaining.

Shouldn’t it be:

self.playButton = self:TouchHandler
self.playButton:addEventListener(“touch”, self.playButton)

Rob,

for the second line it says, function argument expected near ‘self’

Maybe try and move these two functions

function scene:TouchHandler()
function scene:AddEventListeners()
 

before scene:create?  I’m reaching here.  I never attach those types of functions to the scene, just leaving them local to the module itself.

Rob

Rob,

Yeah moving them doesn’t do anything… I did the following

local function TouchFunction(event) scene:TouchHandler(event) end

function scene:AddEventListeners() self.playButton:addEventListener("touch", TouchFunction) end

function scene:TouchHandler(event) print("yay") end

and that worked,   just was concerned I was not doing this right… as in one of my previous posts I was told I was not attaching the events properly… 

http://forums.coronalabs.com/topic/45122-collision-event-not-firing-for-dynamic-object-with-static-object/

I almost never build my  listeners like that.  I prefer the keep it simple silly method:

local function myTouchHandler(event)

end

playButton:addEventListener(“touch”, myTouchHandler)

I personally don’t like table based listeners and prefer function based ones.  Also I understand the value of adding some objects to the scene object so they can be accessed without using upvalues, but in most cases, they don’t need accessed outside and it complicates things.

Maybe it should be:

self.playButton = self.TouchHandler
self.playButton:addEventListener(“touch”, self.playButton)

basically whatever self.playButton is is not a function which is why it’s complaining.