-
Drag in a Straight Line
I know you can restrain an mc in a rectangle when using startDrag, but is there a way to have the mc only go horizontal or vertical without going off diagonally? Sort of like a slider that can only move in a straight line in four directions (like a cross).
Any help is most appreciated.
-
PHP Code:
mc.startDrag (false, new Rectangle (0, mc.y, 500, 0));
Think you can set the y of the rectangle to the MovieClip's y and set the height to 0.
-
Thanks for the reply. Unfortunately that only allows the user to drag horizontally. I also need the option of the mc to be dragged vertically as well. The mc would either be dragged straight up/down OR left/right.
-
Oh I should have read more carefully... well this is what I came up with
PHP Code:
var awd:MovieClip = new MovieClip ();
awd.graphics.beginFill (0x000000);
awd.graphics.drawRect (-20, -20, 40, 40);
awd.graphics.endFill ();
awd.x = stage.stageWidth / 2;
awd.y = stage.stageHeight / 2;
this.addChild (awd);
var startpoint:Point;//The point where you click on the square
awd.addEventListener (MouseEvent.MOUSE_DOWN, onDown, false, 0, true);
function onDown (e:MouseEvent) {
startpoint = new Point (mouseX, mouseY);
awd.addEventListener (MouseEvent.MOUSE_MOVE, onMove, false, 0, true);
}
function onMove (e:MouseEvent) {
awd.removeEventListener (MouseEvent.MOUSE_MOVE, onMove);
var xdiff:Number = Math.abs (startpoint.x - mouseX);//Difference between starting point and the mouse
var ydiff:Number = Math.abs (startpoint.y - mouseY);
if (xdiff > ydiff) {//If x difference is larger than y then you're trying to drag horizontally
awd.startDrag (true, new Rectangle (0, awd.y, stage.stageWidth, 0));
} else {//Or else it's vertically
awd.startDrag (true, new Rectangle (awd.x, 0, 0, stage.stageHeight));
}
}
stage.addEventListener (MouseEvent.MOUSE_UP, onUp, false, 0, true);
function onUp (e:MouseEvent) {
awd.stopDrag ();
}
It's not the most accurate in terms of judging whether you want to move horizontally or vertically but it's a start.
-
Wow. It's more than just a start. I was really struggling with this one. Thank you so much. Now I'm going to mess around with it to get it to work with my current code. Thanks again!