Copy link to clipboard
Copied
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();
}
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 writ
...Copy link to clipboard
Copied
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));
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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?
Copy link to clipboard
Copied
did you try mine?
Copy link to clipboard
Copied
Yes I did, I tried yours first. I posted up above about it.
Copy link to clipboard
Copied
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)
Copy link to clipboard
Copied
Woo HOO! Oh my goodness it worked! Thank you so much Ned! I'm literally smiling ear to ear right now. I knew it had to be something simple like that but I just didn't know what numbers to plug in where and I literally had tried code every which way but as I said I know next to nothing about ActionScript and nothing worked unti now.
Much appreciated!
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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();
}
Find more inspiration, events, and resources on the new Adobe Community
Explore Now