Articles Archive
Articles Search
Director Wiki
 

Group drag sprites

April 12, 1999
by Pat McClellan

Dear Multimedia Handyman,

Is that possible to have hot spots on moveable sprite and can I assign them normal functions like go to frame or marker, play movie/video or others?

Asif Raza Naqvi

Dear Asif,

You could do something like this by maintaining a list of hotspot rects, then altering the list everytime the sprite moves. However, it's fairly easy to do what you want in another way. I'd recommend simply grouping a set of sprites so that they move as a unit when dragged. Then, you can add whatever behaviors you want to the individual sprites in the group.

I think we'll want to have one behavior which gets applied to the draggable sprite (which we'll call "dragger"), then another behavior which will be attached to all the sprites which "tag along" or move with the group. The draggable sprite behavior will have to send out messages to the other sprites in the group, instructing them where and when to move. It occurs to me that you might very well want to have several draggable groups at once, so we'll want to put in some way to address the sprites by group name.

Download a demo movie in Mac or PC format.

In the demo above, I've attached the dragger behavior to sprites 1 and 4, with the tagAlong behavior attached to sprites 2 & 3, and 5 & 6. Note that sprites 1, 2 and 3 are one group ("alpha") and sprites 4, 5 and 6 are the beta group. The buttons (sprites 2, 3, 5 & 6) have a simple behavior attached which does nothing but beep. You could attach any behaviors you wanted to these buttons -- and in fact, these sprites need not be buttons. They could blend with the background sprites under them, be icons, words, whatever.

Let's look at the code for the dragger behavior. I'll use properties to hold values for the starting location of the sprite, the spriteNum, the point at which the mousedown occurs, and the name of the group. Only the group name needs to be specified by the author, so that's the only one appearing in the getPropertyDescriptionList handler.

-- Dragger Behavior
-- copyright © 1999, ZZP Online LLC
-- free use for readers of Director-Online
property pStartLoc, pSprite, pMouseDownLoc, pGroup
on getPropertyDescriptionList me
  set pdlist to [:]
  addprop pdlist, #pGroup, [#comment:"Group name", ¬
    #format:#symbol, #default:#Alpha]
  return pdlist
end getPropertyDescriptionList

on beginSprite me
  set pSprite = the spriteNum of me
  set pStartLoc = the loc of sprite pSprite
end
on mouseDown me
  set pMouseDownLoc = point(the mouseH, the mouseV)
  repeat while the stillDown
    set deltaLoc = pMouseDownLoc - point(the mouseH, ¬
      the mouseV)
    set the loc of sprite pSprite = pStartLoc - deltaLoc
    sendAllSprites(#updateDrag, pGroup, deltaLoc)
    updateStage
  end repeat
end
on mouseUp me
  set pStartLoc = the loc of sprite pSprite
  sendAllSprites(#updateStartLoc, pGroup)
end

When the user mouseDowns on this sprite, the pMouseDownLoc property stores the point where the mouseDown occurred. Now, as the mouse moves around the screen (while stilldown), the difference between the mouses loc and the pMouseDownLoc will be calculated and stored as "deltaLoc". This one value contains all the info regarding distance and direction that we need to move our sprites. This behavior then moves its own sprite by the deltaLoc and then sends out a message to all the other sprites. That message essentially says "hey, if you're in my group, move yourself deltaLoc". After the message goes out, it updates the stage to display the new location of the sprites.

When the user releases the mouse, the mouseUp handler resets pStartLoc to the current (new) location, and sends out another message telling the other sprites in its group to do the same.

The "tagAlong" behavior is a little simpler than the dragger. It starts out the same, setting its group name, spriteNum and starting loc. But it doesn't need a mouseDown or mouseUp handler. (You'll probably have mouseDown and mouseUp handlers in the other behaviors you apply to these tagAlong sprites.) Instead, it'll need a handler to update its location during the drag, and another to update its startLoc property when the drag ends.

-- TagAlong Behavior
-- copyright © 1999, ZZP Online LLC
-- free use for readers of Director-Online
property pStartLoc, pSprite, pGroup
on getPropertyDescriptionList me
  set pdlist to [:]
  addprop pdlist, #pGroup, [#comment:"Group name", ¬
    #format:#symbol, #default:#Alpha]
  return pdlist
end getPropertyDescriptionList
on beginSprite me
  set pSprite = the spriteNum of me
  set pStartLoc = the loc of sprite pSprite
end
on updateDrag me, whichGroup, deltaLoc
  if whichGroup = pGroup then
    set the loc of sprite pSprite = pStartLoc ¬
      - deltaLoc
  end if
end
on updateStartLoc me, whichGroup
  if whichGroup = pGroup then
    set pStartLoc = the loc of sprite pSprite
  end if
end

Notice how easy it is to separate the sprites into distinct groups, just by giving them a group name. When the message goes out to all sprites to updateDrag, then this behavior first checks to see if it is in the right group. If so, it updates its location; if not, it ignores the message. Same thing happens on the updateStartLoc call. These group address concepts are quite handy in a number of applications, so make sure you understand what's going on with it.

Good luck with your project!

Patrick McClellan is Director Online's co-founder. Pat is Vice President, Managing Director for Jack Morton Worldwide, a global experiential marketing company. He is responsible for the San Francisco office, which helps major technology clients to develop marketing communications programs to reach enterprise and consumer audiences.

Copyright 1997-2024, Director Online. Article content copyright by respective authors.