Question

how can i reference a display object's coordinates according to it's parent object or stage from within the class that creates the object?

essentially when i create a new sprite object from a custom class and add it to the display list, i'd like to include code within the custom class that limits the drag coordinates to the stage, or a section of the stage.

//Frame Script
import Swatch;

var test:Sprite = new Swatch();
addChild(test);

___________________

//Custom Class
package
{
import flash.display.Sprite;
import flash.events.MouseEvent;

public class Swatch extends Sprite
    {
    public function Swatch()
        {
        init();
        }

    private function init():void
        {
        var swatchObject:Sprite = new Sprite();

        swatchObject.graphics.beginFill(0x0000FF, 1);
        swatchObject.graphics.drawRect(100, 100, 150, 150);
        swatchObject.graphics.endFill();

        swatchObject.addEventListener(MouseEvent.MOUSE_DOWN, onDrag, false, 0, true);
        swatchObject.addEventListener(MouseEvent.MOUSE_UP, onDrop, false, 0, true);

        this.addChild(swatchObject);
        }

    private function onDrag(evt:MouseEvent):void
        {
        evt.target.startDrag();
        //how to limit it's dragability to the Stage?

        }

    private function onDrop(evt:MouseEvent):void
        {
        evt.target.stopDrag();
        }
    }
}
Was it helpful?

Solution

There is some native support for what you want to do. startDrag() accepts a rectangle as a parameter which restricts the region in which the drag can take place.

function startDrag(lockCenter:Boolean  = false, bounds:Rectangle  = null):void

Hope that helps,

Tyler.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top