Director News
Director Web Sites
Mailing Lists
News Groups
Project Examples
Useful Web Sites

Don't miss these
Wait for a DirectMedia CuePoint(Name as Reference)
Populate MS Combo Box ActiveX
Wait for a Keypress
Amara Flash Photo Animation Software
Check for Network Xtras
Making the Projector Fill the Screen
Video Mask
key navigation
Vector Shape - Create Polygon



Behavior Generic Slider w/Text

Added on 1/27/2004


behavior D6_5 D7 D8 D8_5 D9

This item has not yet been rated

Author: qanimal (website)

I've notcied a definite lack of generic slider scripts in many Director resource sites. There are many, MANY behaviors for volume or movie sliders if that is what you need. There are, however, a lot of other ligitimate uses for a slider which these do not take into account. Examples would be game speed, difficulty level, etc. This behavior will make you a generic slider, record the value in a variable for use in the rest of your movie, and display the slider's current value in a designated text sprite if desired. It is a modified version of Macromedia's default "Volume Slider", just made to be more all-purpose. It looks big, but it is just well-commented. Its not that hard to follow. Use it in good health.

--Macromedia Volume Slider Behavior
--Modified to be a general purpose slider by Dan Snell 1/27/2004
--added ability to put the slider value into a specified text member
--copyright 2004 D5 Enterprises, LLC.

-- Original COMMENTS
-- Channel volume slider
-- Use this behavior on a sprite to create a volume
-- slider for a sound channel
-- v1  30 December 1999 by Darrel Plant
-- v2  08 January 2000 DP, bug 54369
-- v3  02 February 2000 DP & JP, multiple sliders enabled
-- copyright 1999, 2000 Macromedia, Inc.


property constraintSprite   -- sprite to use to determine movement direction and boundaries
property initialSetting      -- setting for slider when movie starts
property pIncrement        -- slider increments - number of integers on slider (i.e. 10, 100, 255, etc.)

property spriteNum
property mySprite
property myActiveFlag  -- has slider been clicked on
property myDirection   -- wich way does slider move
property myBounds      -- upper and lower values for movement
property myClickOffset -- difference between mouse click and sprite loc
property textSprite     -- text sprite to display the current setting of the slider as an integer

property pNewSetting      -- remembered slider setting - this is the slider value you want.  Set this to your global variable for whatever you are using the slider for.


on beginSprite (me)
  mySprite = sprite (spriteNum)
  -- set flag to indicate that sprite has not been clicked on yet
  myActiveFlag = FALSE
  -- get a list of the sprites containing this one
  inList = me.insideSpriteList (spriteNum)
  -- check to make sure the selected constraint
  -- sprite hasn't been moved or deleted
  if getPos(inList, constraintSprite) = 0 then  
    -- chosen sprite is no longer a valid constraint
    -- use the stage instead
    constraintSprite = 0
  end if
  if constraintSprite = 0 then  -- stage is constraint
    boundRect = (the stage).rect
    -- modify rect to start at 0, 0
    boundRect = offset (boundRect, -boundRect.left,
    boundRect = sprite (constraintSprite).rect
  end if
  -- determine whether movement is horizontal or vertical
  -- by comparing bounding rectangle width and height
  boundWidth = boundRect.width
  boundHeight = boundRect.height
  if boundWidth > boundHeight then
    -- width of bounding rect is greater than height
    -- sprite will move horizontally
    -- boundaries are set by using the difference between the
    -- sprite's horizontal loc and sides, in combination
    -- with the bounding rectangle's sides, to determine maximum
    -- and minimum values for the sprite loc, so that the
    -- sprite won't move out of the  rectangle
    myBounds =       [#min: boundRect.left + mySprite.locH - mySprite.left,   #max: boundRect.right + mySprite.locH - mySprite.right]
    -- determine the range of sprite movement, used in calculations
    myBounds[#range] = myBounds.max - myBounds.min
    -- set the sprite direction variable
    myDirection = #horizontal
    -- height of bounding rect is greater than width
    -- sprite will move vertically
    -- boundaries are set by using the difference between the
    -- sprite's vertical loc and the top and bottom, in combination
    -- with the bounding rectangle's top and bottom, to determine maximum
    -- and minimum values for the sprite loc, so that the
    -- sprite won't move out of the  rectangle
    myBounds =  [#min: + mySprite.locV -, #max: boundRect.bottom + mySprite.locV - mySprite.bottom]
    -- determine the range of sprite movement
    myBounds[#range] = myBounds.max - myBounds.min
    -- set the sprite direction variable
    myDirection = #vertical
  end if
  -- remember initial setting, in case you need the initial setting later
  pNewSetting = initialSetting
  -- determine where in the range of movement the sprite
  -- needs to be for the initial setting
  newLoc = myBounds.range * initialSetting / pIncrement
  case myDirection of
      -- move the sprite from left (minimum) to right (maximum)
    #horizontal: mySprite.locH = myBounds.min + newLoc
      -- move the sprite from bottom (maximum) to top (minimum)
    #vertical: mySprite.locV = myBounds.max - newLoc
  end case
  -- send text sprite the integer value of the slider's position
  if textSprite <> 0 then
    sprite(textSprite).member.text = string(pNewSetting)
  end if
end beginSprite


on mouseUpOutside (me)
  -- when the mouse has been released outside of the sprite
  -- reset the activity flag to stop tracking the cursor
  myActiveFlag = FALSE
end mouseLeave

on mouseDown (me)
  -- the slider thumb has been clicked on, determine where the
  -- sprite's top left corner is, how far that is from the point that
  -- was clicked on, and set the activity flag to track the mouse
  myClickOffset = the clickLoc - sprite (spriteNum).loc
  myActiveFlag = TRUE
end mouseDown

on mouseUp (me)
  -- reset the mouse tracking flag when the mouse button is released
  myActiveFlag = FALSE
end mouseUp

on prepareFrame (me)
  if myActiveFlag then
    -- sprite has been clicked on and mouse is being tracked
    -- calculate new location by finding mouse loc and
    -- subtracting the click offset value
    case myDirection of
        newLoc = the mouseLoc.locH - myClickOffset.locH
        newLoc = the mouseLoc.locV - myClickOffset.locV
    end case
    -- make sure new location is in within our bounds
    if newLoc < myBounds.min then
      -- mouse is being dragged below the minumum, reset the new location to the minimum value/spot on the constraint sprite
      newLoc = myBounds.min
      if newLoc > myBounds.max then
        -- mouse is being dragged above the maximum, reset the new location to the maximum value/spot on the constraint sprite
        newLoc = myBounds.max
      end if
    end if
    -- set the sprite location
    case myDirection of
        mySprite.locH = newLoc
        mySprite.locV = newLoc
    end case    
  end if
  -- set slider position
  case myDirection of
      -- horizontal sliders use the distance of the sprite
      -- from the leftmost (minimum) boundary
    #horizontal: offsetLoc = mySprite.locH - myBounds.min
      -- vertical sliders use the distance of the sprite
      -- from the bottom (maximum) boundary
    #vertical: offsetLoc = myBounds.max - mySprite.locV
  end case
  -- change the slider value to a value proportional
  -- to the offset divided by the range times the maximum value (pIncrement)
  if myActiveFlag then
    pNewSetting = pIncrement * offSetLoc / myBounds.range
    --  add setting level as an integer to text sprite if available
    if textSprite <> 0 then
      sprite(textSprite).member.text = string(pNewSetting)
    end if
  end if
end prepareFrame


on insideSpriteList (me, whichSprite)
  -- creates a list of sprites containing
  -- sprite (whichSprite)
  spriteList = [0]
  -- cycle through the sprite channels under this sprite
  thisSprite = sprite (whichSprite)
  repeat with i = 1 to (whichSprite - 1)
    -- check to see what type of sprite it is
    case sprite(i).member.type of
      #animgif, #bitmap, #picture, #QuickTimeMedia, #digitalVideo, #shape, #field, #filmLoop,  #flash, #text, #movie, #vectorShape:
        -- check to make sure potential constraint sprite
        -- is wider or taller
        if ((sprite (i).left < thisSprite.left) and (sprite (i).right > thisSprite.right)) or ((sprite (i).top < and (sprite (i).bottom > thisSprite.bottom)) then
        end if
    end case
  end repeat
  return spriteList
end insideSpriteList


on IsOkToAttach (me, aSpriteType, aSpriteNum)
  -- can be attached to any graphic sprite
  isOK = FALSE
  case aSpriteType of
      isOK = TRUE
  end case
  return isOK
end isOKToAttach

on getPropertyDescriptionList (me)
  props = [:]
  props[#pIncrement] = [#comment: "Slider Max Value (starts at 0):", #format: #integer,  #default: 10]
  props[#constraintSprite] = [   #comment: "Constraining sprite (0 = the stage):",  #format: #integer,   #default: 0,   #range: me.insideSpriteList(the currentSpriteNum)]
  props[#textSprite] = [   #comment: "Text sprite to display setting (0 = none):",   #format: #integer,   #default: 0]
  props[#initialSetting] = [   #comment: "Initial Setting:",   #format: #integer,   #default: 0]
  return props
end getPropertyDescriptionList

on getBehaviorTooltip (me)
  return   "Turns sprite into a sliding 'thumb' for an easily adaptable generic slider with text display."
end getBehaviorTooltip

on getBehaviorDescription (me)
  return  "GENERIC SLIDER W/TEXT OUTPUT" & RETURN & RETURN &   "Drop this behavior onto a graphic sprite to turn it into a sliding 'thumb' that controls a vertical or horiczontal slider." & RETURN & RETURN &   "The slider can be used for any user adjustable value that your movie uses." & RETURN & RETURN &   "The sprite used as the 'thumb' moves horizontally or vertically, with the right or top position being the maximum volume position. " &   "The direction of movement is defined by the sprite chosen as a constraint. " &   "The constraint sprite must be in a lower-numbered channel than the 'thumb', and it must be at least slightly larger than the 'thumb'. " &    "A constraint sprite that is wider than it is tall will cause the 'thumb' to move horizontally; a taller constraint sprite creates a vertical movement. " &   "The stage may also be used as a constraint (and is the default if the chosen constraint has been moved, deleted, or resized). " &   "The value range and the slider's inital setting can be set during authoring." & RETURN & RETURN &   "PERMITTED MEMBER TYPES:" & RETURN &   "Graphics." & RETURN & RETURN &   "PARAMETERS:" & RETURN &   "* Slider Maximum Value- the behavior's range starts at zero and goes up to this value." & RETURN &   "* Constraining sprite - determines direction and distance of slider movement (setting of 0 uses stage for constraint)." & RETURN &   "* Initial Setting - Setting used when slider first appears."&RETURN   "* Text Sprite - text sprite used to display the integer value of the slider setting (not required for use of behavior)."
end getBehaviorDescription  



36 South Court Sq
Suite 300
Newnan, GA 30263

Send e-mail