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
Screen Savers
Set BroadCast Property of a Flash Sprite
Blur Static-Alphamania
Quick mouseover
Populate MS Combo Box ActiveX
PDFCreator Xtra
RavJoystick Xtra
M-Trail v1
Add Your file to windows Startup
123 Flash Menu
 

 

 

Behavior Drag and Switch

Added on 10/2/2000

 

Compatibilities:
behavior D7 D8 Mac PC Shockwave

This item has not yet been rated

Author: JimAndrews (website)

Click and drag sprite to another sprite with this behavior attached to it. The dragged sprite will be topmost among the other sprites that have this behavior when you drag it. Once you drop it on another sprite with this behavior, the script will swap the reg points of the two sprites.

-- DRAG AND SWITCH
-- This behavior is based on the DRAGGABLE behavior in the D8 library and also uses
-- code by John Dowdell at
--http://www.macromedia.com/support/director/ts/documents/mutual_behavior_registratio.htm.
-- Click and drag sprite to another sprite with this behavior attached to it.
-- The dragged sprite will be topmost among the other sprites that have this behavior when you drag it.
-- Once you drop it on another sprite with this behavior, the script will swap the reg points of the two sprites.
-- 16 Nov DP - incorporated JN fix for compatibility with Draw Connector
-- v1 - 7 October 1998 by Darrel Plant
-- Modified 1 February, 1999 by A.M. Kelsey
-- 7 January 2000 Added isOkToAttach J.Powers
-- 27 September 2000 incorporated and extended code from
--http://www.macromedia.com/support/director/ts/documents/mutual_behavior_registratio.htm, Jim Andrews, jim@vispo.com
-- to make dragged sprite topmost and to swap the two sprites.

on getBehaviorDescription me
  return \
    "DragAndSwitch" & RETURN & RETURN & "Makes sprite respond to clicking and dragging the mouse button, similar to setting the draggable property of the sprite. " & "Also, stays topmost among other sprites with this behavior. Dragged sprite will be topmost among all sprites as long as you" & "have fewer than 5000 sprites in your movie. When dropped on another sprite with this behavior, reg points swap." & "Sprite movement may constrained to the stage area." & RETURN & RETURN & "PERMITTED MEMBER TYPES:" & RETURN & "Graphic members" & RETURN & RETURN & "If using with digital video or other sprites in which Direct to Stage is an option, disable Direct to Stage for best results." & RETURN & RETURN & "PARAMETERS:" & RETURN & "* Constrain to stage?"
end getBehaviorDescription

on getBehaviorTooltip me
  return \
    "Makes a sprite draggable. Also, stays topmost among other sprites with this " & "behavior. When dropped on another sprite with this behavior, the two sprites" & "will swap reg points ."
end getBehaviorTooltip

-- PROPERTIES --

property pSprite               -- sprite object
property pLocOffset            -- offset of click from sprite loc
property pActive               -- activity flag
-- author-defined properties
property pConstrained          -- constrain to stage flag

-- The following three properties were added to extend the draggable behavior (in the Dir 8 library) in such a way
-- that the dragged sprite will be topmost among other sprites with this behavior. Also, when the dragged sprite is
-- dragged onto another sprite with this behavior, the script will locate the sprite number of the sprite on which
-- the first was dropped and swap the reg points of the two sprites.
property ourSiblingList --see http://www.macromedia.com/support/director/ts/documents/mutual_behavior_registratio.htm
property myPlace --records the place in ourSiblingList where the current sprite is located
property initialLocation --used to record the location of the dragged sprite when it is first moused down.


-- EVENT HANDLERS --

on beginSprite me
  -- gHighestLocz is used to set locZ of dragged sprite. As long as you don't have 5000 or more
  -- sprites in your movie, the dragged sprite will always be topmost in the movie.
  global gHighestLocZ
  if gHighestLocZ=VOID then
    gHighestLocz=5000
  end if
  --see http://www.macromedia.com/support/director/ts/documents/mutual_behavior_registratio.htm
  --for info on the role of the sendAllSprites line below. Essentially, all sprites with this
  --behavior register on beginSprite to facilitate communications amongst them.
  sendAllSprites(#JRegistrationExample_GetSiblings, [])
  -- determine sprite object reference
  pSprite = sprite (me.spriteNum)
  vMember = pSprite.member
  case vMember.type of
    #animGIF, #flash, #QuickTimeMedia, #digitalvideo, #vectorShape:
      if vMember.directtostage then
        alert "Sprite" && pSprite.spriteNum & \
        ": Direct To Stage media may cause" && \
        "playback problems with the 'Moveable Sprite' behavior."
      end if
  end case
  -- set activity flag
  pActive = FALSE
end beginSprite


on endSprite me
  --see http://www.macromedia.com/support/director/ts/documents/mutual_behavior_registratio.htm
  deleteOne(ourSiblingList, me)
end


on mouseUp me
  -- turn off activity flag when mouse is released
  mDrag FALSE
  --see http://www.macromedia.com/support/director/ts/documents/mutual_behavior_registratio.htm
  --The first sprite in ourSiblingList is checked first, then the second, etc.
  --me.initialLocation is the location of the dragged sprite on mousedown,ie, just before it is dragged.
  call(#SwapTheRegPoints, ourSiblingList[1], me.spritenum, the mouseV, the mouseH, me.initialLocation)
end mouseUp


on mouseDown me
  global gHighestLocz
  --increment to make dragged sprite topmost
  gHighestLocZ=gHighestLocZ +1
  sprite(me.spritenum).locZ=gHighestLocZ
  me.initialLocation=sprite(me.spritenum).loc
  -- turn on activity flag when mouse is clicked
  mDrag TRUE
end mouseUp

on prepareFrame me
  -- if mouse has somehow moved off of sprite and been released
  -- turn off activity flag
  if the mouseUp then mDrag FALSE
  -- if active, move sprite to follow cursor
  if pActive then
    mDragSprite me
  end if
end prepareFrame


-- CUSTOM HANDLERS --

on JRegistrationExample_GetSiblings me, collectionList
  --see http://www.macromedia.com/support/director/ts/documents/mutual_behavior_registratio.htm
  set ourSiblingList to collectionList
  add ourSiblingList, me
  me.myPlace=ourSiblingList.count --this is used in SwapTheRegPoints
end


on SwapTheRegPoints me, caller, mv, mh, initialDragSpriteLocation
  --if the mouse is in the current sprite and the current sprite is not the sprite that was dragged then...
  if (sprite(me.spritenum).right >= mh) and (sprite(me.spritenum).left<=mh) and (sprite(me.spritenum).bottom >= mv) and (sprite(me.spritenum).top<=mv) and (caller<>me.spritenum)then
    --put code here that operates on the caller sprite (the dragged sprite) and the sprite to which ist was dragged.
    --currently what happens is that this code changes the location of the two sprites.
    temp=sprite(me.spritenum).loc
    sprite(me.spritenum).loc=initialDragSpriteLocation
    sprite(caller).loc=temp
  else
    if me.myPlace< ourSiblingList.count then
      --the above condition makes sure no more calls are made when at the end of ourSiblingList.
      --Calls are made to SwapTheRegPoints until the target sprite is found or the end of the list is reached, whichever comes first.
      call(#SwapTheRegPoints, ourSiblingList[me.myPlace+1], caller, mv, mh, initialDragSpriteLocation)
    else
      sprite(caller).loc=initialDragSpriteLocation
    end if
  end if
end

on mDrag vActive
  -- set activity flag
  pActive = vActive
  -- find distance from sprite loc to mouse click
  pLocOffset = pSprite.loc - the mouseLoc
  -- inform other behaviors of current state
  sendAllSprites (#Active_Sprite, the currentSpriteNum * pActive)
end mDrag

on mDragSprite
  -- move sprite to cursor location offset by difference between
  -- original click and sprite loc
  pSprite.loc = the mouseLoc + pLocOffset
  if pConstrained then
    vLeftDiff = pSprite.rect.left
    if vLeftDiff < 0 then
      pSprite.locH = pSprite.locH - vLeftDiff
    end if
    vRightDiff = pSprite.rect.right - the stage.rect.width
    if vRightDiff > 0 then
      pSprite.locH = pSprite.locH - vRightDiff
    end if
    vTopDiff = pSprite.rect.top
    if vTopDiff < 0 then
      pSprite.locV = pSprite.locV - vTopDiff
    end if
    vBottomDiff = pSprite.rect.bottom - the stage.rect.height
    if vBottomDiff > 0 then
      pSprite.locV = pSprite.locV - vBottomDiff
    end if
  end if
end mDragSprite


-- INTER-BEHAVIOR COMMUNICATION

on Forwarded_mouseDown me
  -- turn on activity flag when mouse click is initially
  -- intercepted by another behavior and forwarded
  if not pActive then
    mDrag TRUE
  end if
  return the currentSpriteNum
end Forwarded_mouseDown


-- AUTHOR-DEFINED PARAMETERS --

on isOKToAttach (me, aSpriteType, aSpriteNum)
  return aSpriteType = #Graphic
end

on getPropertyDescriptionList
  vPDList = [:]
  setaProp vPDList, #pConstrained, [#comment: "Constrain to stage", \
    #format: #boolean, #default: TRUE]
  return vPDList
end getPropertyDescriptionList  

 


Contact

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

Send e-mail