zoukankan      html  css  js  c++  java
  • AS3给显示对象拖动支持的工具类

    public class DragUtil
    	{
    		private static var _objVec:Vector.<Sprite> = new Vector.<Sprite>();
    		private static function addToVec(obj:Sprite):Boolean{
    			if(_objVec.indexOf(obj)==-1){
    				_objVec.push(obj);
    				return true;
    			}
    			return false;
    		}
    		private static function removeFromVec(obj:Sprite):void{
    			for(var i:int=0; i<_objVec.length; i++){
    				var sp:Sprite = _objVec[i];
    				if(sp!=null && sp == obj){
    					_objVec.splice(i,1);
    				}
    			}
    		}
    		public static function enableDrag(obj:Sprite):void{
    			if(obj){
    				if(!addToVec(obj)){
    					return;//已存在Drag列表
    				}
    				if(!obj.hasEventListener(MouseEvent.MOUSE_DOWN)){
    					obj.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
    				}
    				if(!obj.hasEventListener(MouseEvent.MOUSE_MOVE)){
    					obj.addEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
    				}
    				if(!obj.hasEventListener(MouseEvent.MOUSE_UP)){
    					obj.addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
    				}
    				if(!obj.hasEventListener(MouseEvent.MOUSE_OUT)){
    					obj.addEventListener(MouseEvent.MOUSE_OUT, onMouseOut);
    				}
    			}
    		}
    		public static function disableDrag(obj:Sprite):void{
    			if(obj){
    				removeFromVec(obj);
    				if(obj.hasEventListener(MouseEvent.MOUSE_DOWN)){
    					obj.removeEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
    				}
    				if(obj.hasEventListener(MouseEvent.MOUSE_MOVE)){
    					obj.removeEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
    				}
    				if(obj.hasEventListener(MouseEvent.MOUSE_UP)){
    					obj.removeEventListener(MouseEvent.MOUSE_UP, onMouseUp);
    				}
    				if(obj.hasEventListener(MouseEvent.MOUSE_OUT)){
    					obj.removeEventListener(MouseEvent.MOUSE_OUT, onMouseOut);
    				}
    			}
    		}
    		private static function onMouseDown(evt:MouseEvent):void {
    			var obj:Sprite = evt.target as Sprite;
    			if(obj!=null && _objVec.indexOf(obj)!=-1){
    				obj.startDrag(false, new Rectangle(0,0, StageProxy.stageWidth()-obj.width, StageProxy.stageHeight()-obj.height));
    			}
    		}
    		
    		private static function onMouseMove(evt:MouseEvent):void {
    			if(evt.buttonDown == false) {
    				var obj:Sprite = evt.target as Sprite;
    				if(obj!=null && _objVec.indexOf(obj)!=-1){
    					obj.stopDrag();
    				}
    			}
    		}
    		
    		private static function onMouseUp(evt:MouseEvent):void {
    			var obj:Sprite = evt.target as Sprite;
    			if(obj!=null && _objVec.indexOf(obj)!=-1){
    				obj.stopDrag();
    			}
    		}
    		
    		private static function onMouseOut(evt:MouseEvent):void {
    			var obj:Sprite = evt.target as Sprite;
    			if(obj!=null && _objVec.indexOf(obj)!=-1){
    				obj.stopDrag();
    			}
    		}
    	}
    
  • 相关阅读:
    vscode配置c++以及美化插件
    自适应辛普森积分法
    [SDOI2014]数表
    [JSOI2009]等差数列
    MUI的踩坑笔记
    笔记:《吴恩达机器学习》——网易云课堂课程[绪论+单变量线性回归]
    CocoStuff—基于Deeplab训练数据的标定工具【五、训练成果分析】
    CocoStuff—基于Deeplab训练数据的标定工具【四、用该工具标定个人数据】
    CocoStuff—基于Deeplab训练数据的标定工具【三、标注工具的使用】
    CocoStuff—基于Deeplab训练数据的标定工具【二、用已提供的标注数据跑通项目】
  • 原文地址:https://www.cnblogs.com/hisiqi/p/2853251.html
Copyright © 2011-2022 走看看