博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
as3 实现树形菜单并带滚动条
阅读量:5083 次
发布时间:2019-06-13

本文共 4394 字,大约阅读时间需要 14 分钟。

最近公司要做一个树形菜单,而as3中根本就没有任何的树形组件。本想自己写一个树形菜单,但因为时间紧急所以花了一个晚上的时间在网上终于找到了一个合适的类改了下就ok啦,自己加了个滚动条并且可以更换树背景。核心类分享出来如下

package tree{	import flash.display.Sprite;	import flash.events.MouseEvent;	public class TreeNode extends Sprite	{		protected var bg:Sprite;		protected var nodeContainer:Sprite;		protected var isOpen:Boolean = true;		protected var isHide:Boolean;		protected var _c:Sprite;		public var isSelected:Boolean = false;		public var nodeArr:Array;		public var _w:int;		public var _h:int;		public var _space:int;		public var pNode:TreeNode;		public var value:Object;		public function TreeNode(container:Sprite,data:Object = null,width:int = 150,height:int = 20,space:int = 0,_isHide:Boolean = false)		{			super();			_c = container ? container : this;			_w = width;			_h = height;			_space = space;			isHide = _isHide;			init();			if(!isHide){				setValue(data);			}		}		private function clickHandler(e:MouseEvent):void{			if(nodeArr.length > 0){				isOpen ? closeList() : openList();			}else{				_c.dispatchEvent(new TreeEvent(TreeEvent.SELECTED,this));			}		}		private function overHandler(e:MouseEvent):void{			if(isSelected){				return;			}			draw(true);		}		private function outHandler(e:MouseEvent):void{			if(isSelected){				return;			}			draw(false);		}		private function selectedHandler(e:TreeEvent):void{			if(nodeArr.length > 0){				return;			}			unSelected();			e.node.selected();		}		private function changeHandler(e:TreeEvent):void{			update();		}		private function unSelected():void{			isSelected = false;			draw();		}		public function getNodeByIndex(index:int):TreeNode{			if(index < 0 || index >= nodeArr.length){				return null;			}			return nodeArr[index];		}		public function addNode(node:TreeNode):void{			if(nodeArr.length > 0){				node.y = nodeArr[nodeArr.length - 1].y + node._h + _space;			}else{				node.y = 0;			}			node.pNode = this;			nodeContainer.addChild(node);			nodeArr.push(node);			_c.dispatchEvent(new TreeEvent(TreeEvent.CHANGE));			if(nodeArr.length == 1){				draw();				bg.buttonMode = true;			}		}		public function removeNode(node:TreeNode):void{			var index:int = nodeArr.indexOf(node);			if(index != -1){				var tempNode:TreeNode = nodeArr.splice(index,1)[0];				nodeContainer.removeChild(tempNode);				tempNode.clear();				_c.dispatchEvent(new TreeEvent(TreeEvent.CHANGE));				if(nodeArr.length == 0){					bg.buttonMode = false;					draw();				}			}		}		public function update():void{			clean();			var currentNode:TreeNode;			var prevNode:TreeNode;			for(var i:int = 0;i < nodeArr.length;i++){				currentNode = nodeArr[i];				currentNode.x = 0;				currentNode.update();				if(i == 0){					currentNode.y = 0;				}else{					prevNode = nodeArr[i-1];					currentNode.y = prevNode.y + prevNode.height + _space;				}				nodeContainer.addChild(currentNode);			}		}		public function clean():void{			for each(var node:TreeNode in nodeArr){				if(node.stage){					nodeContainer.removeChild(node);				}			}		}		public function clear():void{			bg.removeEventListener(MouseEvent.MOUSE_OVER,overHandler);			bg.removeEventListener(MouseEvent.MOUSE_OUT,outHandler);			bg.removeEventListener(MouseEvent.CLICK,clickHandler);			_c.removeEventListener(TreeEvent.CHANGE,changeHandler);			_c.removeEventListener(TreeEvent.SELECTED,selectedHandler);		}		/*		 *应该被继承重写的方法		 */		//树叶节点不用重写		public function openList():void{			isOpen = true;			addChild(nodeContainer);			_c.dispatchEvent(new TreeEvent(TreeEvent.CHANGE));		}		//树叶节点不用重写		public function closeList():void{			isOpen = false;			removeChild(nodeContainer);			_c.dispatchEvent(new TreeEvent(TreeEvent.CHANGE));		}		protected function init():void{			nodeArr = new Array();						bg = new Sprite();			addChild(bg);			draw();						nodeContainer = new Sprite();			nodeContainer.y = _h;			addChild(nodeContainer);						if(isHide){				bg.visible = false;				nodeContainer.y = 0;			}						bg.addEventListener(MouseEvent.MOUSE_OVER,overHandler);			bg.addEventListener(MouseEvent.MOUSE_OUT,outHandler);			bg.addEventListener(MouseEvent.CLICK,clickHandler);			_c.addEventListener(TreeEvent.CHANGE,changeHandler);			_c.addEventListener(TreeEvent.SELECTED,selectedHandler);		}		protected function draw(f:Boolean = false):void{					}		public function selected():void{			isSelected = true;		}		public function setValue(o:Object):void{			value = o;		}	}}

效果图片预览

转载于:https://www.cnblogs.com/chenhaib/archive/2011/07/29/2120782.html

你可能感兴趣的文章
OA权限树搭建 代码
查看>>
正则表达式练习
查看>>
Properties持久化键值对
查看>>
一探前端开发中的JS调试技巧
查看>>
Java中String类型变量初值为null时的连接问题
查看>>
微服务网关解决方案调研和使用总结 专题
查看>>
node.js配置环境变量
查看>>
不适用临时变量交换两个数
查看>>
iframe遮挡bug
查看>>
云标签
查看>>
APUE Chapter 3 - File I/O
查看>>
linux 简单笔记
查看>>
Flask(二)
查看>>
bzoj2480: Spoj3105 Mod
查看>>
写入文件日志方式
查看>>
Markdown编辑器及语法
查看>>
解决DXP软件中off grid的一种最佳方法
查看>>
Hystrix针对不可用服务的保护机制以及引入缓存
查看>>
保险、财务基本知识 一
查看>>
java Web JSTL介绍及基本应用
查看>>