9 Slice Button adding events

I’ve made a 9-Slice button following the docs (http://docs.coronalabs.com/api/library/widget/newButton.html). The button works fine, but I can’t figure out where to add the function that it needs to execute. What do I miss?

--image sheet options and declaration local options = { frames = { { x= 0, y=0, width=21, height=21 }, { x= 21, y=0, width=198, height=21 }, { x= 219, y=0, width=21, height=21 }, { x= 0, y=21, width=21, height=78 }, { x= 021, y=21, width=198, height=78 }, { x= 219, y=21, width=21, height=78 }, { x= 000, y=99, width=21, height=21 }, { x= 021, y=99, width=198, height=21 }, { x= 219, y=99, width=21, height=21 }, { x=240, y=0, width=21, height=21 }, { x=261, y=0, width=198, height=21 }, { x=459, y=0, width=21, height=21 }, { x=240, y=21, width=21, height=78 }, { x=261, y=21, width=198, height=78 }, { x=459, y=21, width=21, height=78 }, { x=240, y=99, width=21, height=21 }, { x=261, y=99, width=198, height=21 }, { x=459, y=99, width=21, height=21 } }, sheetContentWidth = 480, sheetContentHeight = 120 } local buttonSheet = graphics.newImageSheet( "buttonSheet.png", options ) buttonA = widget.newButton { width = botaoLargura, --flexible width of the 9-slice button height = botaoAltura, --flexible height of the 9-slice button sheet = buttonSheet, --reference to the image sheet topLeftFrame = 1, --number of the "top left" frame topMiddleFrame = 2, --number of the "top middle" frame topRightFrame = 3, --etc. middleLeftFrame = 4, middleFrame = 5, middleRightFrame = 6, bottomLeftFrame = 7, bottomMiddleFrame = 8, bottomRightFrame = 9, topLeftOverFrame = 10, topMiddleOverFrame = 11, topRightOverFrame = 12, middleLeftOverFrame = 13, middleOverFrame = 14, middleRightOverFrame = 15, bottomLeftOverFrame = 16, bottomMiddleOverFrame = 17, bottomRightOverFrame = 18, label = "button", font = native.systemFont, labelColor = { default = { 255, 255, 255 }, over = { 0, 0, 0} }, fontSize = 40 } buttonA:setLabel("+") end

You need to add ‘onEvent = myButtonEventHandler’ to the table, making sure you have a function with that name declared first.

I’ve already tried that way. Seems that the button can’t find the function because it was local. Now I remove the local of the function and now works.

Anyway, really thanks! :slight_smile:

Ok - that’s not really the best solution though - it’s a bit like solving the problem of a low ceiling by cutting off your head.

The function should be local, you just need to make sure it is in a place (in scope) where the button can ‘see’ it. If you’re using Composer and are creating these buttons in scene:create, put your listeners above the Composer functions. 

You are right! Just reorganised the scope and now its fine! But what is the need to a function be local?

Thanks Anyway! :)  

When you make something local, it will be automatically cleared from memory when you leave the scope in which is created. In this case, you will probably want this listener to be destroyed when you leave the scene. In your code above, the options table will only remain in memory whilst the function in which it resides is running.

If not, it will stay in memory until you specifically make it nil. 

Sometimes it is necessary to have global functions, but in this case the listener only needs to exist as long as the button exists.

Great stuff, man! Thanks a lot!

You need to add ‘onEvent = myButtonEventHandler’ to the table, making sure you have a function with that name declared first.

I’ve already tried that way. Seems that the button can’t find the function because it was local. Now I remove the local of the function and now works.

Anyway, really thanks! :slight_smile:

Ok - that’s not really the best solution though - it’s a bit like solving the problem of a low ceiling by cutting off your head.

The function should be local, you just need to make sure it is in a place (in scope) where the button can ‘see’ it. If you’re using Composer and are creating these buttons in scene:create, put your listeners above the Composer functions. 

You are right! Just reorganised the scope and now its fine! But what is the need to a function be local?

Thanks Anyway! :)  

When you make something local, it will be automatically cleared from memory when you leave the scope in which is created. In this case, you will probably want this listener to be destroyed when you leave the scene. In your code above, the options table will only remain in memory whilst the function in which it resides is running.

If not, it will stay in memory until you specifically make it nil. 

Sometimes it is necessary to have global functions, but in this case the listener only needs to exist as long as the button exists.

Great stuff, man! Thanks a lot!