I am using horacebury’s script, which can be found here:
https://www.dropbox.com/s/mjmohm8sc7dkgfq/circlelib_modified.zip?dl=0
Here is the code:
-- circle lib require("mathlib") display.setDefault( "isAnchorClamped", false ) local min, max = -10000000000000, 10000000000000 local cache = {} local circlelib = {} local function stroke( obj, colour ) obj.stroke = { type="image", filename="brush2.png" } obj:setStrokeColor( unpack( colour ) ) obj.strokeWidth = 3 end --[[Description: Creates a portion of a circle. Inner and outer radius allow a filled circle portion or a ring to be produced. The x,y position is the centre of the circle when full, meaning the portion may be offset if not a full 360. Parameters: parent: Optional parent display group. x, y: Location of the centre of the circle, not the centre of the output polygon. innerradius: Distance from circle centre of the inside of the ring. outerradius: Distance from circle centre of the outside of the ring - same as the usual radius value. fromangle: Consider north to be 0 and this is where on the circle the portion starts. angle: The radius size of the portion itself. Notes: To get a filled circle use 0 as the inner radius. Examples: A filled pie with the top right corner taken out: newFragment( display.currentStage, 100, 100, 0, 200, 90, 270 ) A full ring with a big hole: newFragment( display.currentStage, 100, 100, 100, 200, 0, 360 ) A half ring: newFragment( display.currentStage, 100, 100, 100, 200, 90, 180 ) The first quarter of a progress ring: newFragment( display.currentStage, 100, 100, 30, 50, 0, 90 )]]-- local function newFragment( parent, x, y, innerradius, outerradius, fromangle, angle ) parent = parent or display.currentStage fromangle = fromangle or 0 angle = (angle or 0) + 1 if (angle \< 1) then angle = 1 elseif (angle \> 361) then angle = 361 end if (outerradius == nil or outerradius \< 2) then outerradius = 2 end if (innerradius == nil or innerradius \< 1) then innerradius = 1 elseif (innerradius \> outerradius) then innerradius = outerradius - 1 end local cachekey = innerradius..":"..outerradius..":"..fromangle..":"..angle local path = {} local points = cache[cachekey] local double = angle\*4 local center = {x=0,y=0} local innerpt = math.rotateTo( {x=0, y=-innerradius}, fromangle, center ) local outerpt = math.rotateTo( {x=0, y=-outerradius}, fromangle, center ) local dim = { xMin=max,yMin=max , xMax=min,yMax=min } if (points == nil) then local function addPathPt( index, inner, outer ) path[index] = outer.x path[index+1] = outer.y path[double-index] = inner.x path[double-index+1] = inner.y if (outer.x \< dim.xMin) then dim.xMin=outer.x end if (inner.x \< dim.xMin) then dim.xMin=inner.x end if (outer.y \< dim.yMin) then dim.yMin=outer.y end if (inner.y \< dim.yMin) then dim.yMin=inner.y end if (outer.x \> dim.xMax) then dim.xMax=outer.x end if (inner.x \> dim.xMax) then dim.xMax=inner.x end if (outer.y \> dim.yMax) then dim.yMax=outer.y end if (inner.y \> dim.yMax) then dim.yMax=inner.y end end addPathPt( 1, innerpt, outerpt ) for i=3, angle\*2, 2 do innerpt = math.rotateTo( innerpt, 1, center ) outerpt = math.rotateTo( outerpt, 1, center ) addPathPt( i, innerpt, outerpt ) end points = math.dedupePoints( path, 2 ) cache[cachekey] = points end local poly = display.newPolygon( 0, 0, points ) dim.xMin, dim.yMin = x+dim.xMin, y+dim.yMin dim.xMax, dim.yMax = x+dim.xMax, y+dim.yMax local dimension = outerradius \* 2 local pathw, pathh = (dim.xMax-dim.xMin), (dim.yMax-dim.yMin) local halfw, halfh = pathw/2, pathh/2 local pathx, pathy = dim.xMin+halfw, dim.yMin+halfh poly.xOffset, poly.yOffset = pathx-x, pathy-y poly.x, poly.y = pathx, pathy local anchorx, anchory = poly:contentToLocal( x+poly.width/2, y+poly.height/2 ) anchorx, anchory = anchorx/poly.width, anchory/poly.height if (cache[cachekey..":anchor"]) then local anchor = cache[cachekey..":anchor"] poly.anchorX, poly.anchorY = anchor.anchorx, anchor.anchory else cache[cachekey..":anchor"] = { anchorx=anchorx, anchory=anchory } poly.anchorX, poly.anchorY = anchorx, anchory end poly.x, poly.y = x, y parent:insert(poly) return poly end circlelib.newFragment = newFragment --[[Description: Creates a radial fill using the newFragment method to generate fills. Can be animated to fill from one amount to another. Parameters: parent: Display group to insert the radial filler into. x, y: Position of the centre of the filled circle. innerradius: Radius of the empty area of the ring - use 0 to have a completely filled circle. outerradius: Outer radius of the complete circle. fill: Colour definition to fill the radial object. Functions: setFillRadius( radius ): Set the fill radius explicitly. setFillTimer( start, finish, time ): Animate smooth filling from the start radius to the finish radius in the given time milliseconds. Notes: Currently the animated filling provided by setFillTimer() does not cater to \<5 degrees. Examples: Create a cyclic object with no parent, located at 200,600 and with a 50px wide hole and a reddish radial fill. local cyclic = circlelib.radial( nil, 200, 600, 25, 75, {1,.3,.4} ) Invert the fill direction from clockwise to anticlockwise. local cyclic = circlelib.radial( nil, 200, 600, 25, 75, {1,.3,.4}, true ) Rotate the radial filler to begin/end filling at east. cyclic.rotation = 90 Set the filled range to 180 degrees - filling from north (at 0) to south (at 180). cyclic:setFillRadius( 180 ) Animate filling from empty to full in 3 seconds. cyclic:setFillTimer( 0, 360, 3000 ) Animate filling from full to empty in 3 seconds. cyclic:setFillTimer( 360, 0, 3000 )]]-- local function radial( parent, x, y, innerradius, outerradius, isanticlockwise ) local group = display.newGroup() parent = parent or display.currentStage fill = fill or {1,1,1} parent:insert( group ) group.x, group.y = x, y if (isanticlockwise) then group.xScale = -1 end if (innerradius == nil or innerradius \< 0) then innerradius=0 end if (outerradius == nil or outerradius \<= innerradius) then outerradius=innerradius+1 end local function createFrame( iswest ) local container = display.newContainer( group, outerradius, outerradius\*2 ) local segment if (iswest) then container.x, container.y = -container.width/2, 0 segment = newFragment( nil, 0, 0, innerradius, outerradius, 0, 180 ) container:insert( segment, true ) segment.x = container.width/2 else container.x, container.y = container.width/2, 0 segment = newFragment( nil, 0, 0, innerradius, outerradius, 180, 180 ) container:insert( segment, true ) segment.x = -container.width/2 end container:insert( segment, true ) container.iswest = iswest container.segment = segment return container end local west, east = createFrame( true ), createFrame( false ) local back = display.newCircle( group, 0, 0, outerradius ) back.isVisible = z back.isHitTestable = true local westtrans, easttrans function group:setRotation( rotation, time, delay ) local function doit() westtrans = transition.cancel( westtrans ) easttrans = transition.cancel( easttrans ) local westrot, eastrot if (rotation \< 0) then rotation=0 elseif (rotation \> 360) then rotation=360 end if (time == nil or time \< 0) then west.segment.rotation = rotation - 180 if (west.segment.rotation \< 0) then west.segment.rotation=0 end east.segment.rotation = rotation if (east.segment.rotation \> 180) then east.segment.rotation=180 end return end -- rotations local currentrot = east.segment.rotation if (west.segment.rotation \> 0) then currentrot=west.segment.rotation+180 end local difference = rotation - currentrot local eastrot = rotation if (eastrot \> 180) then eastrot=180 end local westrot = rotation - 180 if (westrot \< 0) then westrot=0 end local eastdiff = eastrot - east.segment.rotation local westdiff = westrot - west.segment.rotation -- times local millis = math.abs(time / difference) local easttime = math.abs(eastdiff) \* millis local westtime = math.abs(westdiff) \* millis -- transitions if (west.segment.rotation == 0) then -- currently \<180 if (rotation \> 180) then easttrans = transition.to( east.segment, { time=easttime, rotation=eastrot } ) westtrans = transition.to( west.segment, { delay=easttime, time=westtime, rotation=westrot } ) else easttrans = transition.to( east.segment, { time=easttime, rotation=eastrot } ) end else -- currently \>180 if (rotation \> 180) then westtrans = transition.to( west.segment, { time=westtime, rotation=westrot } ) else westtrans = transition.to( west.segment, { time=westtime, rotation=westrot } ) easttrans = transition.to( east.segment, { delay=westtime, time=easttime, rotation=eastrot } ) end end end if (delay and delay \> 0) then timer.performWithDelay( delay, doit, 1 ) else doit() end end function group:setFillColor( ... ) west.segment:setFillColor( unpack( arg ) ) east.segment:setFillColor( unpack( arg ) ) end function group:angleOf( x, y ) local angle = math.angleOf( group, {x=x, y=y} ) + 90 if (angle \< 0) then angle = angle + 360 end if (isanticlockwise) then return 360 - angle end return angle end return group end circlelib.radial = radial return circlelib
and my modified main.lua:
-- circle lib display.setStatusBar( display.HiddenStatusBar ) require("mathlib") local circlelib = require("circlelib") local widget = require( "widget" ) local function createCircle1(event) if ( "ended" == event.phase ) then local pieTimer1 = circlelib.radial( nil, 150, 460, 0, 50, false ) pieTimer1:setFillColor( .41,.39,.25,.45 ) timer.performWithDelay( 0, function() pieTimer1:setRotation( 360, 2000) end, 1 ) end end local function createCircle2(event) if ( "ended" == event.phase ) then local pieTimer2 = circlelib.radial( nil, 300, 460, 0, 50, false ) pieTimer2:setFillColor( .41,.39,.25,.45 ) timer.performWithDelay( 0, function() pieTimer2:setRotation( 360, 2100) end, 1 ) end end local function createCircle3(event) if ( "ended" == event.phase ) then local pieTimer3 = circlelib.radial( nil, 450, 460, 0, 50, false ) pieTimer3:setFillColor( .41,.39,.25,.45 ) timer.performWithDelay( 0, function() pieTimer3:setRotation( 360, 2200) end, 1 ) end end -- Create the widget local button1 = widget.newButton { left = 100, top = 200, id = "button1", label = "One", fillColor = { default={ 0, 1, 0, 1 }, over={ 1, 0.1, 0.7, 0.4 } }, shape="roundedRect", width = 100, height = 40, onEvent = createCircle1 } -- Create the widget local button2 = widget.newButton { left = 250, top = 200, id = "button2", label = "Two", fillColor = { default={ 1, 0, 0, 1 }, over={ 1, 0.1, 0.7, 0.4 } }, shape="roundedRect", width = 100, height = 40, onEvent = createCircle2 }-- Create the widget local button3 = widget.newButton { left = 400, top = 200, id = "button3", label = "Three", fillColor = { default={ 0, 0, 1, 1 }, over={ 1, 0.1, 0.7, 0.4 } }, shape="roundedRect", width = 100, height = 40, onEvent = createCircle3 }
The problem arises after you push one of the buttons and push another button while the pie timer is still cycling. The pie timer you first started stops as the next pie timer is started and so on.
I’m trying to make all three work independently at the same time.