Contents
Articles
Behaviors
Books
Director News
Director Web Sites
FAQ
Games
Mailing Lists
News Groups
Project Examples
Reviews
Software
Tools
Useful Web Sites
Utilities
Xtras

Don't miss these
Irregular MIAW
Change Bitdepth
cXtraTransitionMorphing
cXtraCD
IMidi Xtra
fading button on 1 sprite
cXtraWebBrowser
MP3 player and project source code
SerialXtra
DeadC Scroll
 

 

 

Behavior StopWatch Timer

Added on 7/31/1999

 

Compatibilities:
behavior D7 D8 Mac PC Shockwave

Rating:

Author: RobertWingate

Here"s a quick and easy stopwatch timer you can use for a digital counter. Drop it onto a text or field sprite, and let it go. Start counting automatically, or control it with Lingo. Get the elapsed seconds via the mGetTime method.

-- Stop Watch Timer Behavior
-- v1.0 73199
-- ©1999 Robert K Wingate

-- Here"s a quick and easy stopwatch timer |
-- you can use for a digital counter. Drop |
-- it onto a text or field sprite, and let |
-- it go. Start counting automatically, or |
-- control it with Lingo. Get the elapsed  |
-- seconds via the mGetTime method         |
--                                         |
-- This code is emailware. If you use it,  |
-- email me.  What could be cheaper?       |
-- Send comments to rwingate@mindmagic.com |
--------------------------------------------

property fTicking
property spriteNum, kStartTime
property iLastTime

-----------------------
on beginSprite me
  -- initialize variables and the display
  kStartTime = the milliSeconds
  iLastTime = 0
  mSetCounter me, ":00"
end

-----------------------
on exitFrame me
  -- Check to see if a full seconds has elapsed.
  -- If so, format the new time & fill the field
  if fTicking then
    tix = ( the milliSeconds - kStartTime ) / 1000
    if tix <> iLastTime then
      mSetCounter me, mGetStopWatchTime ( me, tix )
      iLastTime = tix
    end if
  end if
end

-----------------------
on mSetCounter me, displayStr
  -- dump the formatted counter display to the field
  sprite(spriteNum).member.text = displayStr
end

-----------------------
on mGetStopWatchTime me, tim
  -- format the time into a readable text string
  hrs = tim /   3600
  tim = tim mod 3600
  myn = tim /   60
  tim = tim mod 60
  sec = mPadIfNecessary ( me, tim )
  
  case ( TRUE ) of
    ( hrs > 0 ): return hrs & ":" & myn & ":" & sec
    ( myn > 0 ): return             myn & ":" & sec
    otherwise    return                   ":" & sec
  end case
end

-----------------------
on mPadIfNecessary me, sec
  -- private method called by mGetStopWatchTime to
  -- aid in formatting digital counter string
  secStr = string ( sec )
  if secStr.chars.count = 1 then return "0" & secStr
  return secStr
end

-----------------------
on mStartCounting me
  -- public method. Turns counting on
  kStartTime = the milliSeconds
  if not fTicking then fTicking = TRUE
end

-----------------------
on mStopCounting me
  -- public method. Turns counting off
  fTicking = FALSE
end

-----------------------
on mGetTime me
  -- public accessor method. Call it from outside to get
  -- the current seconds elapsed since counting started
  return iLastTime
end

-----------------------
on getPropertyDescriptionList
  -- make sure it"s been dragged onto a field
  if not the currentSpriteNum then exit
  myType = sprite(the currentSpriteNum).member.type
  case ( myType ) of
    #field, #text:
    otherwise:
      err = "This behavior requires text entry. Clear it from this"
      err = err && "sprite, then drag it onto a field or text sprite."
      alert err
      exit
  end case
  
  ls = [:]
  addProp ls, #fTicking, [ #comment:"Start stopwatch on beginSprite?",¬
              #format:#boolean, #default:FALSE ]
  return ls
end

-----------------------
on getBehaviorDescription me
  txt = "Drag this behavior onto a field sprite to turn it"
  txt = txt && "into a stopwatch counter. Use Lingo"s"
  txt = txt && QUOTE & "sendSprite ( whichSprite, #message )"
  txt = txt & QUOTE && "syntax with the behavior"s mStartCounting"
  txt = txt && "methods to turn the stopwatch on and off."
  return txt
end

 


Contact

MMI
36 South Court Sq
Suite 300
Newnan, GA 30263
USA

Send e-mail