package { import flash.events.MouseEvent; import flash.text.TextField; import flash.text.TextFieldAutoSize; import flash.text.TextFormat; import flash.display.DisplayObjectContainer; import flash.display.InteractiveObject; /** * 提示文本 */ public class AS3ToolTip { //目标对象数组 static public var tips : Array = new Array(); public function AS3ToolTip() { } /** * 新建一个提示文本 * * @param owner 要设置提示文本的目标对象 * @param text 提示文本的内容 */ public function create(owner : InteractiveObject, text : String) : void { Create(owner, text); } /** * 新建一个提示文本 * * @param owner 要设置提示文本的目标对象 * @param text 提示文本的内容 */ static public function Create(owner : InteractiveObject, text : String) : void { for(var a in tips) { if(tips[a].owner == owner) { tips[a].text = text; return; } } var toolTip : MyTip = new MyTip(owner, text); tips.push(toolTip); var showToolFunction : Function = function(e : MouseEvent):void { if(owner.stage == null) { return; } owner.stage.addChild(toolTip); owner.addEventListener(MouseEvent.MOUSE_OUT, hideToolFunction); owner.removeEventListener(MouseEvent.MOUSE_OVER, showToolFunction); }; var hideToolFunction : Function = function():void { if(owner.stage == null) { toolTip = null; return; } owner.stage.removeChild(toolTip); owner.addEventListener(MouseEvent.MOUSE_OVER, showToolFunction); owner.removeEventListener(MouseEvent.MOUSE_OUT, hideToolFunction); }; owner.addEventListener(MouseEvent.MOUSE_OVER, showToolFunction); } } } import flash.display.Sprite; import flash.text.TextField; import flash.text.TextFieldAutoSize; import flash.text.TextFormat; import flash.display.InteractiveObject; import flash.events.Event; import flash.events.MouseEvent; class MyTip extends Sprite { public var owner : InteractiveObject; private var toolTip : TextField; private var _text : String; public function get text() : String { return _text; } public function set text(_s : String) : void { _text = _s; toolTip.text = _s; } public function MyTip(_ower : InteractiveObject ,txt : String) : void { owner = _ower; _text = txt; this.addEventListener(Event.ADDED_TO_STAGE, listener); this.addEventListener(Event.REMOVED_FROM_STAGE, relistener); toolTip = new TextField(); toolTip.visible = true; toolTip.text = txt; toolTip.background = true; toolTip.backgroundColor = 0xFFCC66; toolTip.border = true; toolTip.borderColor = 0x000000; toolTip.multiline = false; toolTip.wordWrap = false; toolTip.autoSize = TextFieldAutoSize.LEFT; toolTip.selectable = false; toolTip.mouseEnabled = false; //设置动态文本样式 var format : TextFormat = new TextFormat(); format.font = "_sans"; format.leftMargin = 4; format.rightMargin = 4; format.size = 12; toolTip.setTextFormat(format); this.addChild(toolTip); this.alpha = 0.5; } private function relistener(e : Event) : void { stage.removeEventListener(MouseEvent.MOUSE_MOVE, onMouseMove); } private function listener(e : Event) : void { this.x = this.parent.mouseX + 12; this.y = this.parent.mouseY + 12; stage.addEventListener(MouseEvent.MOUSE_MOVE, onMouseMove); } private function onMouseMove(e : MouseEvent) : void { //trace(1); if(owner.stage != null) { this.x = this.parent.mouseX + 12; this.y = this.parent.mouseY + 12; }else { this.parent.removeChild(this); //stage.removeEventListener(MouseEvent.MOUSE_MOVE, onMouseMove); } } }