• 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();
    			}
    		}
    	}
    
  • 相关阅读:
    ASCII码表以及不同进制间的O(1)转换
    四则运算表达式的语法分析
    语法分析器初步学习——LISP语法分析
    ASC与HEX之间的转换
    字节存储数据
    进制之间的转换
    java多线程CountDownLatch
    java反射机制
    java注解
    使用javafx实现视频播放器
  • 原文地址:https://www.cnblogs.com/hisiqi/p/2853251.html
Copyright © 2020-2023  润新知