Well, you say you want to snap to the edge in your title, but don’t you just mean you want to contrain your drag?
You can do that like this:
Test this script in an empty FlashMX movie:
// Ingore this, its a fix for preventing incorrect Stage-values being returned:
Stage = {width: Stage.width, height: Stage.height};
// Create an Empty MovieClip, and add a lineStyle while we're at it:
_root.createEmptyMovieClip("ball", ++d).lineStyle(25, 0, 100);
// Because our line is very thick, drawing a few pixels will make a big circle:
_root.ball.lineTo(0.5, 1.5);
// If we press on the ball:
_root.ball.onPress = function()
{
// Start dragging it:
startDrag(this, // Drag-target
false, // Do we want to drag the center?
0, // Max-left
0, // Max-top
Stage.width, // Max-right
Stage.height // Max-bottom
);
// Store it:
this.drag = true;
}
// If we release the ball:
_root.ball.onRelease = function()
{
// Stop dragging if we are!
if (this.drag) stopDrag();
// Store it:
this.drag = false;
}
In addition, you’ll need to change the bound, depending on your movieclip. Because right now, the center of the ball will be contrained, not the entire object.
So lets say your window is 100 pixel wide, if you’d drag your center to “x = 0”, the left side of your window would be at -50…
(Understand? Go ahead and check it)
Even that part can be calculated automatically, but before I continue, is this what you mean, or do you really need Snapping?
Cause that wouldn’t be a problem either!
Thanks for you help guys… using the code that you gave me I now have a working version.
I now have 2 questions. one is simple and is marked in the 3rd line of the code below.
The other I dont know if it can be done… When the user drags the box to the edge of the Stage the object stops, but the users mouse (hand) still shows that it is draging an item.
is it possible to force a .stopDrag(); when the object is at the edge of the stage ?
i tried
[AS]if (this._xmouse <0 ) {this.stopDrag();}[/AS]
but could not get it to work.
[AS]
onClipEvent (enterFrame) {
bottom = 400-30; // how can i get bottom to = ( Stage.height - this.height );
top = 30;
right = 550-30;
left = 30;