'event' arg not being passed in

Crikey, I have a slightly odd event listener set up and I’m trying to work out why the ‘event’ argument is not being passed into the handler function. Could someone take a look at my simplified sample, please?

[lua]function createPanel()

local panel = display.newGroup()

function panel:doAction( event )
– event here is nil!
end

function panel:createButton( parent, img, x, y, handler )
local button = display.newImage( parent, img )
img.x, img.y = x, y
button:addEventListener( “touch”, handler )
return button
end

panel.homeButton = panel:createButton( panel, “homebutton.png”, 100, 100, panel.doAction )

return panel

end[/lua] [import]uid: 8271 topic_id: 4499 reply_id: 304499[/import]

most likely because you are calling panel.doAction as your handler not panel:doAction?

[import]uid: 6645 topic_id: 4499 reply_id: 14198[/import]

The handler function gets called, it’s just the event arg is nil when it executes. Would the . affect it? [import]uid: 8271 topic_id: 4499 reply_id: 14255[/import]

Rather than passing in the function for the listener, try passing in the object and giving the object a touch() method.

So instead of
button:addEventListener(“touch”, handler)

you want
button:addEventListener(“touch”, self)

and then rename doAction() to touch()

Refer to this section of the documentation:
http://developer.anscamobile.com/content/events-and-listeners#Function_vs_Table_Listeners


Incidentally, you don’t need to pass an object into its own method, that’s what the value “self” is for. In your code, “parent” is the same as “self”.

refer here http://developer.anscamobile.com/content/introduction#Dot_vs_Colon

And come to think of it you don’t need

panel.homeButton = panel:createButton

since instead of declaring

local button = etc

inside the method you could write

self.button = etc

@knocking Thanks for info, but I’m having trouble refactoring my code to be online with what you’ve said. Would you mind changing the sample I posted to show it please? I come from a C# background and I think here my understanding of ‘this’ is conflicting with ‘self’.

Matt [import]uid: 8271 topic_id: 4499 reply_id: 14338[/import]

I had to make sure this works, no matter what, so I’ve done it by changing:

[lua]function panel:doAction( event )[/lua]

to:

[lua]function doAction( event )[/lua]

Everything else is the same and it works as expected.

I don’t understand why that should affect the execution, especially as the function was getting called, its just the argument was apparently not being passed in.

I needed to have the ‘doAction’ function named that way because the buttons having the listeners attached do not contain the function being fired and there is more than one function, thus excluding the possibility of having it called ‘touch’.

Thanks guys,

Matt. [import]uid: 8271 topic_id: 4499 reply_id: 14346[/import]

The way to refactor the code to what I suggested is the first part of my post:


So instead of
button:addEventListener(“touch”, handler)

you want
button:addEventListener(“touch”, self)

and then rename doAction() to touch()

And by “rename doAction” I mean like a find/replace, so “function panel:doAction(event)” becomes “function panel:touch(event)”

Although…

I needed to have the ‘doAction’ function named that way because the buttons having the listeners attached do not contain the function being fired and there is more than one function, thus excluding the possibility of having it called ‘touch’.

ah I see. Well every situation is different, so now you know multiple ways to go about this. [import]uid: 12108 topic_id: 4499 reply_id: 14360[/import]

Yep, thanks.

Coming from where I do, I like to pass in references to things and have them called later. Its done a lot, of course, with generics, inheritance and so on. (That’s more for anyone whom doesn’t know that, rather than the choir.)

m [import]uid: 8271 topic_id: 4499 reply_id: 14361[/import]