Skip to main content
mhunter1964
Known Participant
March 31, 2015
Answered

constrain drag object

  • March 31, 2015
  • 2 replies
  • 1620 views


Its a ruler type caliper, and I want to be able to slide the jaw. And since it can only move along the ruler, I want to constrain it along the x axis...

Also, I dont want the drop point of the drag object to be left, left upper corner, upper center or any of those. The actual point that has to align to say, the one inch mark, resides about 50 pixles from the left side. Its part three in the top image here:

http://www.craftsmanspace.com/knowledge/vernier-calipers.html

and I want the drag target to align with the left side of  the circle (obect 1) in the second image down

Can anyone help?

Thanks in advance

Message was edited by: sinious - Removed duplicate link

This topic has been closed for replies.
Correct answer Ned Murphy

I did copy the code, but I do think I understand it. I tired this as well as other places to put the var Drag Area but I always get errors:

slide_mc.addEventListener(MouseEvent.MOUSE_DOWN, dragTheObject);

slide_mc.addEventListener(MouseEvent.MOUSE_UP, itemRelease);

slide_mc.buttonMode = true;

var dragArea:Rectangle = new Rectangle(0,0,100,0);//var dragArea added

function dragTheObject(event:MouseEvent):void {

  var item:MovieClip=MovieClip(event.target);

  item.startDrag(false, dragArea);//false, dragArea added to function

  var topPos:uint=this.numChildren-1;

  this.setChildIndex(item, topPos);

}

this gave me two errors:

Scene 1, Layer 'Layer 2', Frame 1, Line 61046: Type was not found or was not a compile-time constant: Rectangle.
Scene 1, Layer 'Layer 2', Frame 1, Line 61180: Call to a possibly undefined method Rectangle.

Try adding the following line before all of what you show...

import flash.geom.Rectangle;

2 replies

sinious
Legend
March 31, 2015

Have a look at the startDrag() docs:

Sprite - Adobe ActionScript® 3 (AS3 ) API Reference

It takes 2 parameters. The first is lockCenter which will lock the mouse to the center of the object or to where the user clicked. Make sure this is false and the user will lock and drop the target aligned differently than how you place it.

The second argument is the bounds the user is allowed to drag within. To restrict it to only X-axis then make sure you give the rectangle no height. e.g. a rectangle that is at x/y 0/0 and allows the user only to move from x:0 to x:100, and not on y: Rectangle(0,0,100,0);

All put together it might look like:

var dragArea:Rectangle = new Rectangle(0,0,100,0);

myDraggableObject.startDrag(false, dragArea);

mhunter1964
Known Participant
March 31, 2015

This is what I have so far:

slide_mc.addEventListener(MouseEvent.MOUSE_DOWN, dragTheObject);

slide_mc.addEventListener(MouseEvent.MOUSE_UP, itemRelease);

slide_mc.buttonMode = true;

function dragTheObject(event:MouseEvent):void {

  var item:MovieClip=MovieClip(event.target);

  var topPos:uint=this.numChildren-1;

  this.setChildIndex(item, topPos);

}

function itemRelease(event:MouseEvent):void {

  var item:MovieClip=MovieClip(event.target);

  item.stopDrag();

  if (dropZone_mc.hitTestPoint(item.x,item.y)) {

  item.x=dropZone_mc.x;

  item.y=dropZone_mc.y;

  this.nextFrame();

  }

else {

  item.x=origX;

  item.y=origY;

  }

};

var origX:Number=slide_mc.x;

var origY:Number=slide_mc.y;

Ned Murphy
Legend
March 31, 2015

You are missing the startDrag() command in the dragTheObject function. 

Ned Murphy
Legend
March 31, 2015

What aspect of this are you having a problem with?  As far as constraining goes, that will be managed within the startDrag function itself via the constraint Rectangle that you specify.  It is not clear to me what you are asking for the rest relative to alignment, but if you wish to have the registration mark other than the top left or whatever then just edit the contents of the slider object so that its registration mark is where you wish it to be.