AS3拖拽管理

之前做项目的时候,需要同时拖拽两个以上的Sprite对象。相当郁闷的是拖拽第二个sprite,或者调用任何一个sprite的stopDrag方法,第一个sprite就失效了。作为Sprite对象的方法,这显然不合适,adobe官方API也没有说明。后来自己写了个DragManager,代码如下

  1 package com.cong.managers
  2 {
  3     import flash.display.Stage;
  4     import flash.display.DisplayObjectContainer;
  5     import flash.utils.Dictionary;
  6     import flash.display.DisplayObject;
  7     import flash.events.MouseEvent;
  8     import flash.geom.Rectangle;
  9 
 10     /**
 11      * @author cong   QQ:19312413
 12      * 
 13      * <example>
 14             DragManager.startDrag(_roomCont, false, new Rectangle(0, 0, 300, 200));
 15              DragManager.stopDrag(_roomCont);
 16      * </example>
 17      */
 18     public class DragManager
 19     {
 20         private static var dragInfo : Vector.<DragData>;
 21 
 22         /**
 23          * @param target  拖拽目标
 24          * @param lockCenter  锁定中心点
 25          * @param rect   拖拽范围
 26          */
 27         public static function startDrag(target : DisplayObject, lockCenter : Boolean = false, rect : Rectangle = null) : void
 28         {
 29             if (dragInfo == null)
 30             {
 31                 dragInfo = new Vector.<DragData>;
 32             }
 33             if (target.stage == null || findDragData(target) != null)
 34             {
 35                 return;
 36             }
 37             var dragData : DragData = new DragData();
 38             dragData.owner = target;
 39             dragData.rect = rect;
 40             dragData.lockCenter = lockCenter;
 41             if(!lockCenter)
 42             {
 43                 dragData.mouseX = target.mouseX;
 44                 dragData.mouseY = target.mouseY;
 45             }
 46             dragInfo.push(dragData);
 47             target.stage.addEventListener(MouseEvent.MOUSE_MOVE, dragHandler);
 48         }
 49 
 50         private static function dragHandler(e : MouseEvent) : void
 51         {
 52             for each (var dragData : DragData in dragInfo)
 53             {
 54                 var target : DisplayObject = dragData.owner;
 55                 target.x = target.parent.mouseX - dragData.mouseX;
 56                 target.y = target.parent.mouseY - dragData.mouseY;
 57 
 58                 if (dragData.rect != null)
 59                 {
 60                     if (target.x > dragData. rect.x + dragData.rect.width)
 61                     {
 62                         target.x = dragData.rect.x + dragData.rect.width;
 63                     }
 64                     else if (target.x < dragData.rect.x)
 65                     {
 66                         target.x = dragData.rect.x;
 67                     }
 68                     if (target.y > dragData.rect.y + dragData.rect.height)
 69                     {
 70                         target.y = dragData.rect.y + dragData.rect.height;
 71                     }
 72                     else if (target.y < dragData.rect.y)
 73                     {
 74                         target.y = dragData.rect.y;
 75                     }
 76                 }
 77             }
 78             e.updateAfterEvent();
 79         }
 80 
 81         private static function findDragData(target : DisplayObject) : DragData
 82         {
 83             for each (var dragData : DragData in dragInfo)
 84             {
 85                 if (dragData.owner == target)
 86                 {
 87                     return dragData;
 88                 }
 89             }
 90             return null;
 91         }
 92 
 93         public static function stopDrag(target : DisplayObject) : void
 94         {
 95             if(dragInfo == null)
 96             {
 97                 return;
 98             }
 99             for (var i : int = 0; i < dragInfo.length; i++)
100             {
101                 if (dragInfo[i].owner == target)
102                 {
103                     dragInfo.splice(i, 1);
104                     if (dragInfo.length == 0)
105                     {
106                         target.stage.removeEventListener(MouseEvent.MOUSE_MOVE, dragHandler);
107                     }
108                     break;
109                 }
110             }
111         }
112     }
113 }
114 import flash.display.DisplayObject;
115 import flash.geom.Rectangle;
116 
117 class DragData
118 {
119     public var owner : DisplayObject;
120     public var lockCenter : Boolean;
121     public var rect : Rectangle;
122     public var mouseX : int;
123     public var mouseY : int;
124 }

 

转载于:https://www.cnblogs.com/cc523/archive/2013/05/31/3109510.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值