[Use-revolution] Re: Constraining Movement

Dave Cragg dcragg at lacscentre.co.uk
Tue Nov 27 17:38:00 EST 2001


At 2:01 pm -0600 27/11/01, Ken Ray wrote:
>Scott,
>
>I like yours because you can "slide" around the edges of the bounding
>rectangle. However, the mouseDown-ed object's loc still snaps to the
>mouseLoc. Here's a revised version that doesn't do that:
>
>on mouseDown
>   set the uAllowDrag of me to true
>   set the dX of me to (the mouseH - item 1 of loc of me)
>   set the dY of me to (the mouseV - item 2 of loc of me)
>end mouseDown
>
>on mouseMove
>   if not the uAllowDrag of me then exit mouseMove
>   put the mouseH - (the dX of me) into offsetMouseH
>   put the mouseV - (the dY of me) into offsetMouseV
>   set the loc of me to \
>       min(max(offsetMouseH,left of img myImg + (width of me/2)),\
>       right of img myImg - (width of me/2)),\
>       min(max(offsetMouseV,top of img myImg + (height of me/2)),\
>       bottom of img myImg - (height of me/2))
>end mouseMove
>
>on mouseUp
>   set the uAllowDrag of me to false
>end mouseUp
>
>on mouseRelease
>   mouseUp
>end mouseRelease
>
>I love this collaboration! :-)

Me too.  :)

One small revision. I think it's faster to use the mouseMove 
parameters instead of mouseV, mouseH.

on mouseMove pX, pY
   if not the uAllowDrag of me then exit mouseMove
   put pX - (the dX of me) into offsetMouseH
   put pY - (the dY of me) into offsetMouseV
   set the loc of me to \
       min(max(offsetMouseH,left of img myImg + (width of me/2)),\
       right of img myImg - (width of me/2)),\
       min(max(offsetMouseV,top of img myImg + (height of me/2)),\
       bottom of img myImg - (height of me/2))
end mouseMove

Also, previous examples used globals for storing the offset values. 
Scott uses custom properties. I generaly use locals declared outside 
the handlers (script locals). I think globals are probably not a good 
idea here, but has anyone checked the performance difference of using 
custom properties versus script locals? When you're draggin' and 
checkin' stuff at the the same time, every millisecond helps.

cheers
Dave Cragg







More information about the use-livecode mailing list