Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

constrain drag object

Community Beginner ,
Mar 31, 2015 Mar 31, 2015


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

TOPICS
ActionScript
1.5K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

LEGEND , Apr 01, 2015 Apr 01, 2015

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

import flash.geom.Rectangle;

Translate
LEGEND ,
Mar 31, 2015 Mar 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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Mar 31, 2015 Mar 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);

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Mar 31, 2015 Mar 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;

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Mar 31, 2015 Mar 31, 2015

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Mar 31, 2015 Mar 31, 2015

that was just an oops. The real code is this:

function dragTheObject(event:MouseEvent):void {

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

  item.startDrag();

  var topPos:uint=this.numChildren-1;

  this.setChildIndex(item, topPos);

}

Everything works except I am not sure where the Constraint code goes

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Mar 31, 2015 Mar 31, 2015

Follow the information/example Jason provided.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Apr 01, 2015 Apr 01, 2015

Trying to figure out where I add the code to what I have.

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);

  item.startDrag();

  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;

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

//myDraggableObject.startDrag(false, dragArea);

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Apr 01, 2015 Apr 01, 2015

I take it you have just copied someone else's code and have no idea what you are working with.  Look for the startDrag code in what you already had and try to work out what you need to do to revise it with the info you have been given.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Apr 01, 2015 Apr 01, 2015

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.
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Apr 01, 2015 Apr 01, 2015

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

import flash.geom.Rectangle;

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Apr 01, 2015 Apr 01, 2015
LATEST

that was it. Now I just need to stop it from jumping when it snaps to the target. But I will try to figure that out on my own...

Thanks!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines