【问题标题】:How to use a movieclip as a boundary for another dragable object in AS3?如何使用影片剪辑作为 AS3 中另一个可拖动对象的边界?
【发布时间】:2019-04-05 07:15:45
【问题描述】:

如何使用一个movieclip作为另一个可拖动对象的边界?

我所知道的是,我们可以在开始拖动时使用矩形作为边界。

dragable_mc.addEventListener(MouseEvent.MOUSE_DOWN, start_drag);
function start_drag(e:MouseEvent)
{
    var rect:Rectangle = new Rectangle(0,0,100,100);
    dragable_mc.startDrag(false, rect);
}

as3怎么把一个movieclip拖到另一个movieclip in flash中? (就像我在图片中显示的那样)

【问题讨论】:

    标签: actionscript-3 flash air adobe


    【解决方案1】:

    如果它是动态形状,则必须在拖动时记录每帧可拖动对象的 x,y 坐标。然后使用边界进行位图命中点测试以检查对象是否超出边界。如果在外面,返回上一个没有越界的坐标。

    编辑

    需要重命名的两个变量是dragTarget和bound_mc

    dragTarget 是你的 dragable_mc

    bound_mc 是边界的影片剪辑的名称。

    bound_mc 需要为 png 格式,并且越界区域必须是透明的。例如:

    import flash.geom.Point;
    import flash.geom.Rectangle;
    import flash.events.MouseEvent;
    import flash.events.Event;
    import flash.display.BitmapData;
    
    stop();
    
    var bmd:BitmapData =new BitmapData(600, 400, true, 0x000000);
    var rect:Rectangle;
    var lastPt:Point = new Point();
    
    function init():void {
        rect = new Rectangle(0, 0, stage.stageWidth, stage.stageHeight);
        setUpBitmap();
    }
    
    function setUpBitmap():void {
        bmd.draw(bound_mc);
    
        dragTarget.addEventListener(MouseEvent.MOUSE_DOWN, start_drag);
    }
    
    function start_drag(event:MouseEvent):void {
        dragTarget.removeEventListener(MouseEvent.MOUSE_DOWN, start_drag);
        stage.addEventListener(MouseEvent.MOUSE_UP, stop_drag);
    
        lastPt.x = dragTarget.x;
        lastPt.y = dragTarget.y;
    
        dragTarget.startDrag(false, rect);
        this.addEventListener(Event.ENTER_FRAME, logPoint);
    }
    
    function stop_drag(event:MouseEvent):void {
        this.removeEventListener(Event.ENTER_FRAME, logPoint);
        stage.removeEventListener(MouseEvent.MOUSE_UP, stop_drag);
        dragTarget.addEventListener(MouseEvent.MOUSE_DOWN, start_drag);
    
        dragTarget.stopDrag();
    }
    
    function logPoint(event:Event):void {
        var curPoint:Point = new Point(stage.mouseX, stage.mouseY);
    
        if ( bmd.hitTest(new Point( bound_mc.x, bound_mc.y ), 0, curPoint) ) {
            lastPt = curPoint;
        } else {
            dragTarget.x = lastPt.x;
            dragTarget.y = lastPt.y;
    
            stage.dispatchEvent(new MouseEvent(MouseEvent.MOUSE_UP));
        }
    }
    
    init();
    

    【讨论】:

    • 我试试你的想法,我应该说它行不通,hitTestObject 在我的边界周围使用一个矩形来检查 hit 。
    • bitmapdata.hittest,不是 hittestobject。生病看看我是否稍后写一个例子。 help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/…
    • 哦,对不起,让我再试一次;)
    • @user1234567,谢谢......它工作得很好:)。重要的是我们应该将bound_mc的注册点设置在左上角。
    • @VC.One 是的,很好。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-01-13
    • 1970-01-01
    • 2014-09-23
    • 2017-06-24
    • 2013-01-27
    • 2015-07-07
    • 2010-09-11
    相关资源
    最近更新 更多