如何在 AS3 中使用影片剪辑作为另一个可拖动对象的边界?
How to use a movieclip as a boundary for another dragable object in AS3?
如何将动画片段用作另一个可拖动对象的边界?
据我所知,我们可以在开始拖动时使用矩形作为边界。
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中将一个movieclip拖到另一个movieclip中的方法是什么?
(就像我在图片中显示的那样)
如果它是动态形状,则必须在拖动时的每一帧记录可拖动对象的 x、y 坐标。然后对边界进行位图命中点测试,以检查对象是否越界。如果在外面,return到最后一个没有越界的坐标。
编辑:
您需要重命名的两个变量是 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();
如何将动画片段用作另一个可拖动对象的边界?
据我所知,我们可以在开始拖动时使用矩形作为边界。
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中将一个movieclip拖到另一个movieclip中的方法是什么?
(就像我在图片中显示的那样)
如果它是动态形状,则必须在拖动时的每一帧记录可拖动对象的 x、y 坐标。然后对边界进行位图命中点测试,以检查对象是否越界。如果在外面,return到最后一个没有越界的坐标。
编辑:
您需要重命名的两个变量是 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();