Multi-rect Sprite Constraint
March 7, 1999
by Pat McClellan
Dear Multimedia Handyman,
Can I make a sprite constrained within more than one sprite?
Dana Benson
Dear Dana,
Nice direct question. So let's get right to the answer. No.
At least, not using Director's built-in constraint of sprite Lingo. However, we can fake it pretty easily with a behavior. We'll call it a multi-rect contstraint behavior. Let's make it so that the author can specify 2 sprites, within which our movable sprite will be constrained. So, our properties will include pSpriteA and pSpriteB; a pFlag property which will be set to either #move when the sprite is being dragged, or #stop; plus pRectA and pRectB, properties to store the rects of pSpriteA and pSpriteB. (And, of course, a property to hold the moving sprite's spriteNum.)
The author will specify pSpriteA and pSpriteB when the behavior is dropped on the sprite. All the other properties are assigned automatically in the beginSprite handler. The mouseDown and mouseUp handlers simply change the state of pFlag (from #stop to #move and back.)
The real action happens on the exitFrame handler. In this handler, we're going to set a variable (mouseLoc) to point(the mouseH, the mouseV). Then -- assuming pFlag is set to #move -- we'll check to see if mouseLoc is within either of the specified rects. If it is inside either one, then we'll set the loc of the moving sprite to mouseLoc. If the mouse wanders outside the given rects, then the position of the moving sprite is not updated.
Here's the code.
-- Multirect Constraint -- copyright © 1999, ZZP Online, LLC property pSpriteA, pSpriteB, pRectA, pRectB, pFlag, pMySprite on getPropertyDescriptionList me set pdlist to [:] addprop pdlist, #pSpriteA, [#comment:"First Constraining ¬ Sprite", #format:#integer, #default:1] addprop pdlist, #pSpriteB, [#comment:"Second Constraining ¬ Sprite", #format:#integer, #default:2] return pdlist end getPropertyDescriptionList on beginSprite me set pRectA = the rect of sprite pSpriteA set pRectB = the rect of sprite pSpriteB set pMySprite = the spriteNum of me set pFlag = #stop end on mouseDown me set pFlag = #move end on mouseUp me set pFlag = #stop end on mouseUpOutside me set pFlag = #stop end mouseUpOutside on exitFrame me set mouseLoc = point(the mouseH, the mouseV) if pFlag = #move then if inside(mouseLoc, pRectA) or inside(mouseLoc,pRectB) then set the loc of sprite pMySprite = mouseLoc end if end if end
If you want to have more than 2 sprites, it's a simple matter to add more sprites/rects to the behavior. Good luck with your program.
Copyright 1997-2024, Director Online. Article content copyright by respective authors.