UPDATE #2 (1/23/2015): Fixed a bug that only manifests in Corona SDK build #2015.2544 and later. In build #2015.2544, Corona Labs fixed the finalize() bug that prevented objects’ finalize events from being triggered when their parent display group was removed. I had utilized a brilliant workaround developed by Sergey Lerg, but there was an error in my implementation of his fix that caused a crash when run on build 2015.2544 and later. I fixed that crash by adding a 1ms timer when checking to see if finalize events are being triggered as they should. The fix is not necessary for up-to-date Corona builds, but I’m leaving it in for folks who might be running legacy Corona builds.
UPDATE #1 (1/7/2015): The progressRing module has been updated to ensure that the procedurally-generated mask image reliably meets the requirements for mask images. Previously, it didn’t always work when running on devices whose screen size did not match up with the content width/height defined in an app’s config.lua file.
Something that is occasionally asked about on the Corona Labs forums is how to achieve a circular progress indicator (what I call a “progress ring”). The uses for this type of widget are limitless, but here are a few examples:
- player/enemy health bars in a RTS-style game (see Ravenmark)
- countdown timer to show when a player’s turn is up in a turn-based multiplayer or puzzle game
- a more visually interesting percentage indicator than the usual horizontal or vertical bar (in a business app)
A number of solutions have been proposed in the forums, often involving the use of sprite objects with X number of frames to cycle through depending on the position of the progress bar. These solutions can work, of course, but I set out to create my own progress ring solution with the following goals in mind:
- The movement of the progress ring should be seamless and smooth, regardless of which direction it was moving and/or how quickly (something that can’t happen with sprite-based solutions unless you use a prohibitively high number of frames)
- The progress ring must be created solely with code, so that it can be modularized into a single lua file and added to a project with one line of code (again, something that isn’t possible with sprite-based solutions).
- The progress ring must be visually customizable through code: colors, size, depth of the “donut hole” (or no donut hole at all), stroke width, etc.
- A progress ring object must have simple and easy-to-use methods for controlling it after it’s been created.
And I’m happy to report that I’ve come up with a solution that meets all those requirements and makes it very easy to add a progressRing to any Corona project with just one line of code! READ MORE »