Code I

on beginSprite (me)
-- store sprite and member
pSprite = sprite(me.spritenum)
pMem = pSprite.member
-- store original image of the member
pOrigImage = pMem.image.duplicate()
-- create property list with on-digit information
pOnDigit = [#color: pOnDigitColor, #width: pOnDigitWidth, #height: pOnDigitHeight, #image: 0]
-- create property list with off-digit information
pOffDigit = [#color: pOffDigitColor, #width: pOffDigitWidth, #height: pOffDigitHeight, #image: 0]
-- initialize the timer digits
me.initDigits()
-- initialize the timer
me.initTimer()
end beginSprite

on endSprite (me)
-- restore original image of the member
pMem.image = pOrigImage
end endSprite

Code II

-- init the timer display
on initTimer (me)

-- create timer image
me.createTimer()

-- reset elapsed time
pCurrentTime = 0

-- start timer if autostart flag is true
if pAutoStart then 
me.mStartTimer()
end if

end initTimer

Code III

-- create the timer image
on createTimer (me)

-- check the orientation of the timer
case pOrientation of

#horizontal:
-- calculate the width based on digit width, duration and gap
myWidth = (pOnDigit.width * pDuration) + (pGap * (pDuration-1))
myHeight = pOnDigit.height
pIncRect = rect(pOnDigit.width + pGap, 0, pOnDigit.width + pGap, 0)

#vertical:
-- calculate the height based on digit height, duration and gap
myWidth = pOnDigit.width
myHeight = (pOnDigit.height * pDuration) + (pGap * (pDuration-1))
pIncRect = rect(0, pOnDigit.height + pGap, 0, pOnDigit.height + pGap)

end case

-- create the timer image
pImage = image(myWidth, myHeight, 16)

-- check the direction the timer is moving
case pDirection of
#up:
mySrcImg = pOffDigit.image
#down:
mySrcImg = pOnDigit.image
end case

-- copy the digits into the timer image
repeat with i = 1 to pDuration
mySrcRect = mySrcImg.rect
myDestRect = mySrcRect + (pIncRect * (i-1))
pImage.copyPixels(mySrcImg, myDestRect, mySrcRect)
end repeat

-- update the member image
me.updateImage()

end createTimer

Code IV

-- draw a single step of the timer
on drawTimerStep (me)

-- check the direction the timer is moving
case pDirection of
#up:
mySrcImg = pOnDigit.image
mySrcRect = mySrcImg.rect
myDestRect = mySrcRect + (pIncRect * (pCurrentTime - 1))
#down:
mySrcImg = pOffDigit.image
mySrcRect = mySrcImg.rect
myDestRect = mySrcRect + (pIncRect * (pDuration - pCurrentTime))
end case

-- copy the new digit into the timer image
pImage.copyPixels(mySrcImg, myDestRect, mySrcRect)

-- update the member image
me.updateImage()

end drawTimerStep

Code V

-- start the timer
on mStartTimer (me)

-- check if there is already a timeout object
if NOT(objectP(pTimer)) then
-- create a new timeout object to animate the timer
pTimer = timeout("Graphical Timer - Sprite " & me.spritenum).new(pInterval, #mStepTimer, me)
else
-- start a paused timer
pTimer.period = pInterval
end if

end mStartTimer

Code VI

-- timer animation step
on mStepTimer (me)

-- increment elapsed time
pCurrentTime = pCurrentTime + 1

-- draw current step
me.drawTimerStep()

-- check if animation has finished
if pCurrentTime >= pDuration then

-- stop the timer
me.mStopTimer()

-- call Lingo function
if pCall <> "" then
do pCall
end if

end if

end mStepTimer