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

问题描述 投票:1回答:1

如何使用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在flash中的另一个动画片段中拖动动画片段的方法是什么? (就像我在图片中所示)enter image description here

actionscript-3 flash air adobe
1个回答
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();
© www.soinside.com 2019 - 2024. All rights reserved.