Hi All,

I now have that silly little animate function I was threatening earlier. I post it here for comments. Is this done in a Livecode-style? Are there CPU hogs I'm hiding in here? Can anyone spot any errors? Any improvements/speed-ups you can suggest?

I noticed that the regular expressions are long, and are going to wrap, which means they'll likely break in many email clients. Just tape 'em back together.

The syntax came out pretty much as I'd hoped:

animate "long name of obj", \
      "property newvalue;[property newvalue[;...]]", \
      howLong

"property" is the name of the property of the object.

"newvalue" is the new value of the property.  It can be
    an integer, a float, a percentage of current (50%), or
    a color triplet (255,0,0)

"howLong" is the time to attempt to take, in milliseconds to
    perform the animation.

Caveats:
1) No error/bounds/property checking happens. If you tell it to, it will try to set the "vanilla" property of something whether it exists or not. 2) The color changing "works" for some value of "works". It's slowish because it uses regular expressions.
3) The frames per second is hard coded into animate.  Change as you like.
4) There's no hand-shaking with the world outside this function. You would *not* want to try to make a game with this function. 5) It relies on the 'send' command for each frame of each animated object, which means it's much less efficient than it could be.

----------------------------8<-----------------------------
-- pAtts and pTime is only passed the first time.
-- subsequent calls only pObj is populated, all working
-- variables are stored as custom properties in pObj.
on animate pObj,pAtts,pTime
   lock screen
   local nf
   local sFramesPerSecond
   put 30 into sFramesPerSecond

   put the animating of pObj into bAnimating
   if bAnimating is empty or not bAnimating then
      -- set up animating
      -- make sure we always get at least 1 frame
      put max(trunc((sFramesPerSecond*pTime/1000)+.5),1) into nf

      set the pAtts of pObj to empty

      split pAtts by ";" and space
      set the pAtts of pObj to pAtts
      repeat for each line aKey in the keys of pAtts
         put the aKey of pObj into tVar

         put aKey&"_delta" into tKey
         if char -1 of pAtts[aKey] = "%" then
            -- we were passed a percentage
            put (char 1 to -2 of pAtts[aKey])/100 into tValue
set the tKey of pObj to (tValue * (the aKey of pObj) - the aKey of pObj)/nf
         else if aKey = "foregroundColor" or aKey = "backgroundColor" then
put matchText(pAtts[aKey],"([0-9\.\-]+),([0-9\.\-]+),([0-9\.\-]+)",tNRed,tNGreen,tNBlue) into tJunk
            put the aKey of pObj into tColor
            if tColor is empty then
               put 0,0,0 into tColor
            end if
put matchText(tColor,"([0-9\.\-]+),([0-9\.\-]+),([0-9\.\-]+)",tORed,tOGreen,tOBlue) into tJunk
            put (tNRed - tORed)/nf into tDRed
            put (tNGreen - tOGreen)/nf into tDGreen
            put (tNBlue - tOBlue)/nf into tDBlue
-- set the color change here...note that each channel can be fractional.
            set the tKey of pObj to tDRed&comma&tDGreen&comma&tDBlue
         else
            set the tKey of pObj to (pAtts[aKey] - the aKey of pObj)/nf
         end if

-- Drat! floats get trunced, so we must carry them through, but start
         -- with the original number.
         put aKey&"_float" into tKeyFloat
         set the tKeyFloat of pObj to the aKey of pObj

         set the timeslice of pObj to pTime/nf
         set the numberOfFrames of pObj to nf
         set the curFrame of pObj to 1
      end repeat
      set the animating of pObj to true
      send "animate "&pObj to me in 1/nf milliseconds
   else
      -- we're animating, so the delta's should exist
      if the curFrame of pObj <= the numberOfFrames of pObj then
         set the curFrame of pObj to (the curFrame of pObj + 1)
         put the pAtts of pObj into pAtts
         repeat for each line aKey in the keys of pAtts
            put aKey&"_delta" into tKey
            put aKey&"_float" into tKeyFloat

            if aKey = "foregroundColor" or aKey = "backgroundColor" then
               put the tKeyFloat of pObj into tColor
put matchText(tColor,"([0-9\.\-]+),([0-9\.\-]+),([0-9\.\-]+)",tORed,tOGreen,tOBlue) into tJunk
               put the tKey of pObj into tColor
put matchText(tColor,"([0-9\.\-]+),([0-9\.\-]+),([0-9\.\-]+)",tNRed,tNGreen,tNBlue) into tJunk
               put tNRed + tORed into tDRed
               put tNGreen + tOGreen into tDGreen
               put tNBlue + tOBlue into tDBlue
set the aKey of pObj to trunc(tDRed)&comma&trunc(tDGreen)&comma&trunc(tDBlue) set the tKeyFloat of pObj to tDRed&comma&tDGreen&comma&tDBlue
            else
               -- Drat! floats get trunced, so we must carry them through.
set the aKey of pObj to the tKeyFloat of pObj + the tKey of pObj set the tKeyFloat of pObj to the tKeyFloat of pObj + the tKey of pObj
            end if
         end repeat
         send "animate "&pObj to me in (the timeslice of pObj) milliseconds
      else
         set the animating of pObj to false
      end if
   end if
end animate

_______________________________________________
use-livecode mailing list
use-livecode@lists.runrev.com
Please visit this url to subscribe, unsubscribe and manage your subscription 
preferences:
http://lists.runrev.com/mailman/listinfo/use-livecode

Reply via email to