Search code examples
actionscript-3flashflash-cs6

How to drag a symbol only horizontally?


I'm currently trying to make a pretty simple drag and drop Flash program. I made a program in which you can drag a square with your mouse.

However I would like the square only to move horizontally. I've been trying to find something on the internet pretty long now, without a finding a solution. So I thought maybe you guys could help me...

Here's what I've done:

I first made a square and made a symbol of it named: "blok"

Then I wrote the following code within the same scene:

var myblock:Sprite = blok;

this.addChild(myblock);
myblock.addEventListener(MouseEvent.MOUSE_DOWN, startMove);

function startMove(evt:MouseEvent):void 
{
myblock.startDrag();
}

myblock.addEventListener(MouseEvent.MOUSE_UP, stopMove);

function stopMove(e:MouseEvent):void 
{
myblock.stopDrag();
}

Solution

  • startDrag takes a bounding box parameter. Try this:

    function startMove(evt:MouseEvent):void 
    {
      myblock.startDrag(false, new Rectangle(0, myblock.y, 1000, myblock.y));
    }
    

    The 0 and 1000 are min and max x values, substitute whatever you need to use in your application.

    Adobe documentation for Sprite class