all groups > flash actionscript > february 2006 >
You're in the

flash actionscript

group:

startDrag parameters question


startDrag parameters question MonkeyPiePdx
2/3/2006 11:58:21 PM
flash actionscript:
Can someone explain how the parameters work in startDrag? I have a large MC
that I want to be able to drag around, but I don't want - (for example) the
left edge of the MC to go past the left edge of the window . . . or into the
window. Or said another way, I want the user to be able to move the MC around,
but never see the edges. Is this possible and how do I do this.

The help says: left, top, right, bottom Values relative to the coordinates of
the movie clip?s parent that specify a constraint rectangle for the movie clip.
These parameters are optional. "Relative to WHAT? How does this work?

Thanks!
Re: startDrag parameters question tralfaz
2/4/2006 10:55:32 AM
[quoted text, click to view]

You said 'window' so I am not sure if you meant the stage or if you
have a smaller box on your stage that is the window.
If you had a viewing window on stage with an instance name of 'box'
with a size of 400w by 400h and it is positioned at 100x and 100x,
your draggable clip can go to the left to position 100, and up to
position 100. In the right direction you can go to the x position of
the box (100) plus the width of the box (400) minus the width of your
draggable clip. In the down direction the limit would be the box._y
plus the box._height minus the draggable clip's height. See if this
code helps. It is written out in a long form..

// box is a movieclip on stage.. (the viewing window)
onClipEvent (mouseDown) {
var leftEdge = _root.box._x;
var topEdge = _root.box._y;
var rightEdge = (_root.box._x + _root.box._width) - this._width;
var botEdge = (_root.box._y + _root.box._height) - this._height;
startDrag( this,true,leftEdge, topEdge, rightEdge, botEdge);
}

onClipEvent(mouseUp)
{
stopDrag();
}

//********************************************************
OK.. now, having told you that, all those numbers are based on both
movieclips having a top-left registration point. When you create a
movieclip you can set it's registration point to 9 different places.
Later on you can alter the registration point by editting the
movieclip and dragging it around. Once you move either clip from the
top-left registration point the calculations have to take that into
account.

When you drag an object around with it locked to the mouse pointer, it
doesn't look good to drag it by the top-left corner, so usually you
would set the draggable clip's registration point to the center
instead of top-left. When you do that, the calculations are just a
little bit different. You have to add half of the draggable clip's
width to the top and left limit and subtract half of the draggable
clip's height from the y limit. The viewing window 'box' still has
top-left registration.

onClipEvent (mouseDown) {
var leftEdge = _root.box._x + (this._width / 2);
var topEdge = _root.box._y + (this._height / 2);
var rightEdge = (_root.box._x + _root.box._width) - (this._width /
2);
var botEdge = (_root.box._y + _root.box._height) - (this._height
/ 2);

startDrag( this, true, leftEdge, topEdge, rightEdge, botEdge);
}

onClipEvent(mouseUp)
{
stopDrag();
}


//********************************************************
If the entire stage is your viewing window then the code is like this
(in long form)

onClipEvent (mouseDown) {
var leftEdge = 0 + (this._width / 2);
var topEdge = 0 + (this._height / 2);
var rightEdge = Stage.width - (this._width / 2);
var botEdge = Stage.height - (this._height / 2);
startDrag( this,true,leftEdge, topEdge, rightEdge, botEdge);
}

onClipEvent(mouseUp)
{
stopDrag();
}
//********************************************************

If your registration points get off center or off-corner then you can
either fix the registration points or add a correction value in x and
y.
hope that helps,
tralfaz






AddThis Social Bookmark Button