how to target scene:function() in EventListeners

I’m using Storyboard in my app and it works really nice, but I do have one problem though, I’m using display.newImage() as a button with an addEventListener to create buttons. I’m not using the ButtonWidget because I need to tween the buttons.
Problem is that I can’t seem to target a “local” function of the storyboard scene as a Listener.

For Example:

[lua]button:addEventListener( “touch”, scene:onButtonEvent )

function scene:onButtonEvent(event)
– do something
end
[lua]My workaround was to do this:
[lua]button:addEventListener( “touch”, function(event) scene:onButtonEvent(event) end )[/lua]

This situation is not ideal because in this case the removeEventListener doesn’t work
[lua]button:removeEventListener( “touch”, function(event) scene:onButtonEvent(event) end )[/lua]

Any ideas how to tackle this problem?
[import]uid: 143756 topic_id: 31444 reply_id: 331444[/import]

move the function code above the event listener code.
[import]uid: 19626 topic_id: 31444 reply_id: 125673[/import]

Moving the function above the event listener doesn’t fix the problem.
An error is thrown when I write the EventListener like this:
button:addEventListener( “touch”, scene:onButtonEvent )
Runtime error: error loading module ‘scrn_start’ from file scrn_start.lua’:
scrn_start.lua:219: function arguments expected near ‘)’ [import]uid: 143756 topic_id: 31444 reply_id: 125723[/import]

move the function code above the event listener code.
[import]uid: 19626 topic_id: 31444 reply_id: 125673[/import]

Moving the function above the event listener doesn’t fix the problem.
An error is thrown when I write the EventListener like this:
button:addEventListener( “touch”, scene:onButtonEvent )
Runtime error: error loading module ‘scrn_start’ from file scrn_start.lua’:
scrn_start.lua:219: function arguments expected near ‘)’ [import]uid: 143756 topic_id: 31444 reply_id: 125723[/import]

Is there a reason that you’re making the onButtonEvent function a method of “scene”?

I would rewrite it like this:

[lua]
function button:touch (event)
– do something
end

button:addEventListener( “touch”, button )[/lua]

Then you can remove the listener like so:

[lua]button:removeEventListener( “touch”, button )[/lua]

If you need the touch function to be more general/reusable, you could write it like this:

[lua]local function buttonTouch (self, event)
– do something
end

– Store a reference to the buttonTouch function
button.touch = buttonTouch

button:addEventListener( “touch”, button )[/lua]

To remove the listener:
[lua]button:removeEventListener( “touch”, button )[/lua]

I’m still new, so I could be wrong, but hopefully this will get you going in the right direction. I tested both methods and they worked.

Good luck!

  • David [import]uid: 149111 topic_id: 31444 reply_id: 126076[/import]

Thanks for your advice David, you made me think about the setup of my code. I think one of the problems is that I didn’t declare the function before calling it. I guess I still have to switch from OOP coding :slight_smile:
I’m now refactoring the code and am confident that I’m going to get it to work.
Thanks again! [import]uid: 143756 topic_id: 31444 reply_id: 126160[/import]

Glad to be of help! [import]uid: 149111 topic_id: 31444 reply_id: 126195[/import]

Is there a reason that you’re making the onButtonEvent function a method of “scene”?

I would rewrite it like this:

[lua]
function button:touch (event)
– do something
end

button:addEventListener( “touch”, button )[/lua]

Then you can remove the listener like so:

[lua]button:removeEventListener( “touch”, button )[/lua]

If you need the touch function to be more general/reusable, you could write it like this:

[lua]local function buttonTouch (self, event)
– do something
end

– Store a reference to the buttonTouch function
button.touch = buttonTouch

button:addEventListener( “touch”, button )[/lua]

To remove the listener:
[lua]button:removeEventListener( “touch”, button )[/lua]

I’m still new, so I could be wrong, but hopefully this will get you going in the right direction. I tested both methods and they worked.

Good luck!

  • David [import]uid: 149111 topic_id: 31444 reply_id: 126076[/import]

Thanks for your advice David, you made me think about the setup of my code. I think one of the problems is that I didn’t declare the function before calling it. I guess I still have to switch from OOP coding :slight_smile:
I’m now refactoring the code and am confident that I’m going to get it to work.
Thanks again! [import]uid: 143756 topic_id: 31444 reply_id: 126160[/import]

Glad to be of help! [import]uid: 149111 topic_id: 31444 reply_id: 126195[/import]