如何在 AS3 中为可拖动对象定义动态边界?

How to define dynamic boundaries for a dragable object in AS3?

我的视频播放器上有两个控件可以选择起点和终点,请参考附图

这些控件的名称分别是inpoint_mcscrub_outpoint_mc,我添加了Listener函数来为两个控件拖动自身

this.controls_mc.inpoint_mc.addEventListener(MouseEvent.MOUSE_DOWN, this.startScrubbingIN);
            this.controls_mc.scrub_outpoint_mc.addEventListener(MouseEvent.MOUSE_DOWN, this.startScrubbingOUT);

private function startScrubbingIN(_arg1:MouseEvent){
            trace("scrubBarIsMovingIN");
            this.cueCard.stage.addEventListener(MouseEvent.MOUSE_UP, this.stopScrubbingIN);
            this.cueCard.stage.addEventListener(MouseEvent.MOUSE_MOVE, this.scrubBarIsMovingIN);
            this.scrubbing = true;
            var _local2:Rectangle = new Rectangle(this.controls_mc.progressBar_mc.x, this.controls_mc.inpoint_mc.y, this.controls_mc.progressBar_mc.width, 0);
            this.controls_mc.inpoint_mc.startDrag(false, _local2);

        }

        private function startScrubbingOUT(_arg1:MouseEvent){
            this.cueCard.stage.addEventListener(MouseEvent.MOUSE_UP, this.stopScrubbingOUT);
            this.cueCard.stage.addEventListener(MouseEvent.MOUSE_MOVE, this.scrubBarIsMovingOUT);
            this.scrubbing = true;
            var _local2:Rectangle = new Rectangle(this.controls_mc.progressBar_mc.x, this.controls_mc.scrub_outpoint_mc.y, this.controls_mc.progressBar_mc.width, 0);
            this.controls_mc.scrub_outpoint_mc.startDrag(false, _local2);

        }

我的目标是,我不希望它们彼此超越,这意味着 inpoint_mc 只能拖动到 scrub_outpoint_mc,而 scrub_outpoint_mc 只能拖动到 inpoint_mc

您需要根据另一个擦洗点的位置来计算拖动矩形。您的矩形现在包含 progressBar_mc 下的区域,现在您必须根据 inpoint_mcscrub_outpoint_mc 的位置 trim 它。为此,您需要更改用于限制 startDrag() 的矩形的 xwidth

    private function startScrubbingIN(_arg1:MouseEvent){
        trace("scrubBarIsMovingIN");
        this.cueCard.stage.addEventListener(MouseEvent.MOUSE_UP, this.stopScrubbingIN);
        this.cueCard.stage.addEventListener(MouseEvent.MOUSE_MOVE, this.scrubBarIsMovingIN);
        this.scrubbing = true;
        var _local2:Rectangle = new Rectangle(this.controls_mc.progressBar_mc.x, this.controls_mc.inpoint_mc.y, 
            this.controls_mc.scrub_outpoint_mc.x-this.controls_mc.progressBar_mc.x, 0);
        // now we're limiting in point to current position of out point
        this.controls_mc.inpoint_mc.startDrag(false, _local2);

    }

    private function startScrubbingOUT(_arg1:MouseEvent){
        this.cueCard.stage.addEventListener(MouseEvent.MOUSE_UP, this.stopScrubbingOUT);
        this.cueCard.stage.addEventListener(MouseEvent.MOUSE_MOVE, this.scrubBarIsMovingOUT);
        this.scrubbing = true;
        var _local2:Rectangle = new Rectangle(this.controls_mc.inpoint_mc.x, this.controls_mc.scrub_outpoint_mc.y, 
            this.controls_mc.progressBar_mc.width+this.controls_mc.progressBar_mc.x-this.controls_mc.inpoint_mc.x, 0);
        // the same for out, but the width of the rectangle is calculated to include x offset
        this.controls_mc.scrub_outpoint_mc.startDrag(false, _local2);

    }