开发者

Limit display object's drag coordinates in AS3

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 flas开发者_开发知识库h.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();
        }
    }
}


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜