如何将动画片段用作AS3中另一个可拖动对象的边界? [英] How to use a movieclip as a boundary for another dragable object in AS3?

查看:142
本文介绍了如何将动画片段用作AS3中另一个可拖动对象的边界?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How to use a movieclip as a boundary for another dragable object?

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

All I know is, we can use a rectangle for boundary in start drag .

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中另一个影片剪辑中的方式是什么?
(如我在图片中所示)

What is the way to drag a movieclip in another movieclip in flash by as3? (like as I shown in pic)

推荐答案

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

If it is a dynamic shape, you'd have to log the x,y coordinates of the drag-able object every frame upon drag. Then do a bitmap-hitpoint test with the boundary to check if the object is out of bounds. If it is outside, return to the last coordinates that isn't out of bound.

编辑

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

The two variables you need to rename are dragTarget and bound_mc

dragTarget是您的dragable_mc

dragTarget is your dragable_mc

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

bound_mc is the name of the movieclip of your boundary.

bound_mc必须为png格式,出界区域必须透明。示例:

bound_mc need to be in png format, and the out of bounds area MUST be transparent. 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();

这篇关于如何将动画片段用作AS3中另一个可拖动对象的边界?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆