Skip to main content
Known Participant
July 25, 2013
Answered

Using Drag & Drop with Movie Clip

  • July 25, 2013
  • 2 replies
  • 1732 views

Through ActionScript 3 I am trying to create a slider for a tablet.  I have 6 images grouped into a movie clip symbol with the instance name of images.  I just want the user to be able to drag the images right or left to view them.  This works although it doesn't stay in the bounding rectangle I want it to.  It's basically free floating and can be dragged off the stage.  To be honest I don't know much about ActionScript and have spent days on forums looking at ActionScript and trying to implement it into my own but nothing seems to work.  I just want the user to be able to drag right or left and not have the movie clip move up or down or beyond my stage to the left or right.  Below is my code if anyone could help I'd greatly appreciate it.

images.addEventListener(MouseEvent.MOUSE_DOWN, fl_ClickToDrag_2);

function fl_ClickToDrag_2(event:MouseEvent):void

{

   images.startDrag(false,new Rectangle(0,0,stage.stageWidth,150));

}

stage.addEventListener(MouseEvent.MOUSE_UP, fl_ReleaseToDrop_2);

function fl_ReleaseToDrop_2(event:MouseEvent):void

{

    images.stopDrag();

}

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

I did try to put my x and y coordinates in, in place of the word number and also my heighth in but I still get that syntax error and it wont do anything. Where it says width and heighth is that the height and width of my movie clip or the stage?


You place the values in place of everything in that Rectangle constructor.  Look at the code you showed in the first posting, that is how you specify the values.  You do not just replace the "number" word... it should be like this...

new Rectangle(0, 0, 766, 0)

But you need to adjust the values.  If the object jumps up when you click down, then the y value should not be 0.  It needs to match the position of the object as it is.

(I did not see your reply to me as you were writing it while I was writing mine)

2 replies

Ned Murphy
Legend
July 25, 2013

The Rectangle that you define in the startDrag function call is where you specify the boundaries for the dragging.  The rectangle is specified as follows....

Rectangle(x:Number = 0, y:Number = 0, width:Number = 0, height:Number = 0)

So if you do not want the object to be dragged up or down, you need to set the y value for wherever it needs to be vertically, and the height value to 0 so that it has nowhere to move.

You'll need to determine the x and the width values.  It is possible you want the x value to start at a negative value to define when the object is all the way to the left as far as you want it to go, and then define the width based on it being all the way to the other extreme.

Known Participant
July 26, 2013

Ned thank you so much for your reply!  I appreciate it.  I put the bit of code you gave me in and now I am getting a syntax error saying 1084:Syntax error: expecting rightparen before colon.  My code looks like this now.

images.addEventListener(MouseEvent.MOUSE_DOWN, fl_ClickToDrag_2);

function fl_ClickToDrag_2(event:MouseEvent):void

{

   images.startDrag(false,new Rectangle(x:Number = 0, y:Number = 0, width:766 = 0, height:Number = 0));

}

stage.addEventListener(MouseEvent.MOUSE_UP, fl_ReleaseToDrop_2);

function fl_ReleaseToDrop_2(event:MouseEvent):void

{

    images.stopDrag();

}

Participating Frequently
July 25, 2013

you want to subtract the images width from stageWidth to keep it from sliding off the stage.

im thinking it should look like

images.startDrag(false,new Rectangle(0,0,images.width-stage.stageWidth,0));

Known Participant
July 26, 2013

Thank you for your response I really appreciate it!  I gave this a try and it's close to working.  When you mouse down the movie clip shoots up and the bottom of it lines up with the center of the stage.  From there you are able to drag it around but not all the way to the end of the movie clip and you can drag it too far to the right.

Ned Murphy
Legend
July 26, 2013

As already indicated you need to adjust the properties that you specify for the Rectangle... think about it.  You need to adjust the y property of the Rectangle to be where the object's y is if you do not want it to jump.  You need to specify the x and the width to control/limit the lateral movement.