如何将动画片段用作AS3中另一个可拖动对象的边界?

时间:2019-04-05 07:15:45

标签: actionscript-3 flash air adobe

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

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

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将影片剪辑拖到Flash中的另一个影片剪辑中的方式是什么? (如我在图片中所示) enter image description here

1 个答案:

答案 0 :(得分:2)

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

修改

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

dragTarget是您的dragable_mc

bound_mc是边界的动画片段的名称。

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

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();