AS3的一个提示类

http://25swf.blogbus.com/logs/13625313.html

 

 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);
  }
 }
}

 

使用方法 AS3ToopTip.create(mymc,"this is mymc");

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值