Are you sick of ui.lua ? Sick of it with director class? Here is the answer!
I was tired of ui.lua making my code slower and unstable so created the MovieClip Button library.
It uses movieClip.lua and supports the same button types as ui.lua (button pressed, button released).
However! it doesn’t stop there.
Here are the supported features :
Each button can have it’s own unique sound effect.
The button’s touch listener is removed after successful execution of the button press. This prevents crashing and code from getting executed more times than it is supposed to.
The library handles everything for you so you only have to set up a few variables and the rest is done.
Each button can have its own custom function executed for “began” and “released” states.
Example usage :
Please see included samples (screen1.lua or screen2.lua) or continue reading below
In your main.lua file include these lines :
local movieclip = require("movieclip")
uiButton = require("buttonLib")
Then you set up buttons like so :
newGameBTN = movieclip.newAnim({"newGame.png", "newGame2.png"}) -- The sequence of images : Should contain a default and pressed state image
newGameBTN.x = display.contentWidth / 2 -- The x position of the button
newGameBTN.y = 140 -- The y position of the button
newGameBTN.sound = clickSND -- The pointer to the button sound effect (each button can have it's own unique sound effect
--The Two below variables are not required if you are not using the director class
newGameBTN.sceneChangesTo = "screen2" -- The required scene to change to on touch
newGameBTN.sceneChangesWithEffect = "moveFromRight" -- The required effects to apply to the scene change
newGameBTN.beganFunction = newGameBeganFunction -- The pointer to the custom function to execute on button began state (If any) Optional
newGameBTN.releasedFunction = newGameReleasedFunction -- The pointer to the custom function to execute on button began state (If any) Optional
localGroup:insert(newGameBTN)
--Add the event listener for the button
newGameBTN:addEventListener("touch", uiButton.handleEvent)
After that the library handles everything else for you.
(I will submit it it the codebase, however i get an error : Compatibility: illegal value.) Isn’t it just the build number that is in there ? ie : 243 ?)
If you like it or need help using it, please leave a comment! [import]uid: 6981 topic_id: 6986 reply_id: 306986[/import]
Realized that you may not always want to remove a button listener straight after using it (say in a options menu) so you can now specify if the listener should get auto removed upon touch. If not you can remove the listener yourself (in a back button listener for instance)
syntax :
removeListenerOnTouch
usage :
newGameBTN.removeListenerOnTouch = true – If true the library will automatically remove the event listener for you when it is done.
Sorry I just took the link offline while i tested thoroughly on the device as well as the simulator.
Just want to be 100% certain it is bug free. Seems like it is working great now, just doing some final testing to confirm then the download link will be back
Edit : It passed the testing and is now available to download again. If you downloaded a previous version of this library please discard it and download the new version here : www.infuseddreams.com/demo/ButtonLib_V0.4.zip [import]uid: 6981 topic_id: 6986 reply_id: 24671[/import]
I would like to see a better API for this. Something more standard, like:
[lua]button = buttonLib.newMovieButton{
images = {“newGame.png”, “newGame2.png”},
x = display.contentWidth / 2,
y = 140,
sound = clickSND,
scene = “screen2”,
effect = “moveFromRight”,
beganFunction = newGameBeganFunction,
releasedFunction = newGameReleaseFunction }
localGroup:insert(button)[/lua]
Just something more like what people are already used to doing in Corona.
Also, not sure why you break the Began and Released functions into two calls rather than using the standard event handler syntax. Again, making this more like normal Corona code would be nice.
I actually use a ui.lua that I modified myself to also include the button Width and Height properties. I like to use display.newImageRect(image,w,h) to load images so that I have better support for different device resolutions. This lets Corona load high resolution versions of the buttons for iPads or Android HD devices. So that’s something else you might want to consider.
[import]uid: 12529 topic_id: 6986 reply_id: 24697[/import]