TipBox for flash

      Flash习作 2007-3-6 14:12

/**
* 2007-03-05
* @author jh7086
* @version 0.1
*/
class TipBox {
 private var TT1;
 private var TT2;
 private var MC:MovieClip;
 private var TF:TextField;
 private var setting:Object;
 private static var $ this:TipBox;
 private function TipBox() {
  trace("创建TipBox");
  setting = {time1:500, time2:2000, background:true, backgroundColor:0xFFFFCC, border:true, depth:1000};
  MC = _root.createEmptyMovieClip("mc_1", _root.getNextHighestDepth());
  MC.createTextField("tf_1", 10, 0, -300, 0, 0);
  TF = MC.tf_1;
  TF.autoSize = true;
  TF.background = setting.background;
  TF.backgroundColor = setting.backgroundColor;
  TF.border = setting.border;
 }
 public static function getInstence():TipBox {
  if ($ this == null) {
   $ this = new TipBox();
  }
  return $ this;
 }
 public function showTip(msg:String, t1:Number, t2:Number) {
  if (msg != "" && msg != null) {
   t1 = t1 ? t1 : setting.time1;
   t2 = t2 ? t2 : setting.time2;
   $ this.TT1 = setInterval(function () {
    $ this.TF.text = msg;
    $ this.TF._x = _xmouse;
    $ this.TF._y = _ymouse+20;
    $ this.TT2 = setInterval(function () { $ this.hideTip();clearInterval($ this.TT2);}, t2);
    clearInterval($ this.TT1);
   }, t1);
  }
 }
 public function hideTip() {
  clearInterval(TT1);
  clearInterval(TT2);
  TF._x = 0;
  TF._y = -300;
 }
 public function addTip(target, msg:String) {
  target.onRollOver = function() {
   $ this.showTip(msg);
  };
  target.onRollOut = function() {
   $ this.hideTip();
  };
 }
 public function getsetting(obj:Object) {
  for (var i in setting) {
   if (obj[i] != null && typeof (obj[i]) == typeof (setting[i])) {
    setting[i] = obj[i];
   }
  }
  MC.swapDepths(setting.depth);
  TF.background = setting.background;
  TF.backgroundColor = setting.backgroundColor;
  TF.border = setting.border;
 }
}

//--------------------------用法示例--------------------------------------------
//假设有_root中有名为myBtn,myBtn2的按钮或是MC,在_root中加上以下几句,即可实现tip效果

var tipBox:TipBox = TipBox.getInstence();
tipBox.addTip(myBtn,"我是按钮")
tipBox.addTip(myBtn2,"我是另一个按钮")

//若要设置文本框格式,延迟时间,可以用getsetting()方法
//{}内的属性可以只写需要改变的,但名称要写正确,getsetting()方法会忽略掉不存在的属性设置
tipBox.getsetting({time1:10, time2:5000, backgroundColor:0xFF0000})

//具体应用效果http://blog.5d.cn/vip/jh7086/200703/371295.html

标签集:TAGS:
回复Comments() 点击Count()

回复Comments

{commentauthor}
{commentauthor}
{commenttime}
{commentnum}
{commentcontent}
作者:
{commentrecontent}