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();
    			}
    		}
    	}
    
  • 相关阅读:
    MySQL删除重复数据
    C#如何实现Object与byte[]的互相转换
    远程桌面连接(转)
    WEB标准学习路程之"CSS":2.字体font
    WEB标准学习路程之"CSS":3.背景Background属性
    WEB标准学习路程之"入门篇":8.XHTML代码规范
    WEB标准学习路程之"CSS":9.常用选择符
    WEB标准学习路程之"CSS":1.什么是样式表
    WEB标准学习路程之"入门篇":9.校验及常见错误
    WEB标准学习路程之"CSS":4.尺寸Dimensions属性
  • 原文地址:https://www.cnblogs.com/hisiqi/p/2853251.html
Copyright © 2011-2022 走看看