最近公司要做一个树形菜单,而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; } }}
效果图片预览