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
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]
@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’.
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]
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]