0 Replies Latest reply on May 25, 2006 1:21 AM by nurbdog

    newbie: tweaking Directors 'Follow Sprite' behaviour

    nurbdog
      Greetings All!

      I'm attempting to adjust/tweak the library behaviour 'Follow Sprite'. I need it to NOT center the following sprite over its target...but rather align at a particular offset in pixels [eg align the left side of the chaser sprite to the right side of the target]

      ...i am a newbie and have spent many hours staring at and adjusting the lingo to try and return the right values...with no joy. i know its probably very simple...if anyone can help me it would be much appreciated

      code which controls positioning [from what i can understand] within 'Follow Sprite' is as below:
      --------------------------------------------------------------------------------

      -- CUSTOM HANDLERS --

      on mCenter vRect
      -- returns point value representing center of rectangle
      vHalfWidth = vRect.width / 2
      vHalfHeight = vRect.height / 2
      return point (vRect.left + vHalfWidth, vRect.top + vHalfHeight)
      end mCenter

      on mCenterOffset vRect
      -- returns vector for difference between center of sprite and loc;
      -- used to ensure compatibility with text, vector, and shape sprites
      -- as well as bitmaps with offset loc values
      vCenter = mCenter (vRect)
      return vCenter - sprite (pSprite).loc
      end mCenterOffset

      on mCenteredRect vPoint, vRect
      --returns a rect centered on point vPoint
      vWidth = vRect.width
      vHeight = vRect.height
      vHalfWidth = vWidth / 2
      vHalfHeight = vHeight / 2
      vLeft = vPoint.locH - vHalfWidth
      vTop = vPoint.locV - vHalfHeight
      return rect (vLeft, vTop, vLeft + vWidth, vTop + vHeight)
      end mCenteredRect

      on mInitialize me
      -- called from beginsprite
      -- sets initial values for all conditions
      pSprite = me.spriteNum
      pMaxSpeed = 1000 -- determines maximum allowable speed
      pSpeed = min (pMaxSpeed, max (1, integer (pSpeed)))
      -- prevents entry of values out of range
      pOrigin = sprite (pSprite).loc + mCenterOffset (sprite (pSprite).rect)
      pDestination = mCenter (sprite (pFollow).rect)
      pOrigTime = the milliseconds -- current time on millisecond clock
      pDestTime = pOrigTime + 1
      pPeriod = 1
      pVector = point (0, 0)
      pMoving = FALSE -- sprite begins at rest
      pStageBounds = point (the stage.rect.width, the stage.rect.height)
      mSetDest me, pDestination
      end mInitialize

      on mSetDest me, vDest
      if pActive then
      -- only changes destination if sprite is following cursor
      pDestination = vDest
      -- find center of sprite
      pOrigin = sprite (pSprite).loc + mCenterOffset (sprite (pSprite).rect)
      -- determines vector from origin to destination
      pVector = pDestination - pOrigin
      -- find total distance to travel from origin to destination
      vDistance = mVectorLength (pVector)
      -- period is the time needed to move from origin to destination
      -- in milliseconds; vDistance is in pixels, pSpeed represents
      -- pixels per second, multiplied by 1000 milliseconds / pixel;
      -- making operations in this order means all calculations are
      -- performed with integers
      pPeriod = max (1, 1000 * vDistance / pSpeed)
      -- reset time of movement start
      pOrigTime = the milliseconds
      -- this property isn't necessary (it could be calculated when
      -- needed) but this prevents recalculation
      pDestTime = pOrigTime + pPeriod
      -- sprite should begin moving toward destination
      pMoving = TRUE
      end if
      end mSetDest

      -----------------------------------------------------------------------