最近做了个Flash网站,要实现鼠标移上去显示文章标题,所以写了这个类。
/**
* @author:chaoren
* @用途:热区显示提示文字
* @date:2009.07.18
* @example:
import com.chaoren.utils.ToolTip;
ToolTip.init(this);
ToolTip.register(this.mc, "提示文字");
*/
package com.chaoren.utils{
import flash.accessibility.AccessibilityProperties;
import flash.display.*;
import flash.events.*;
import flash.geom.Point;
import flash.text.*;
public class ToolTip extends Sprite {
private static var instance:ToolTip=null;//只实例化一次
private var labelTF:TextField;
private var area:DisplayObject;
public function ToolTip() {
labelTF=new TextField ;
labelTF.autoSize=TextFieldAutoSize.LEFT;
labelTF.selectable=false;
labelTF.multiline=false;
labelTF.wordWrap=false;
labelTF.defaultTextFormat=new TextFormat("宋体",16,0x666666);
labelTF.text="提示信息";
labelTF.x=5;
labelTF.y=2;
addChild(labelTF);
redraw();
visible=false;
mouseEnabled=mouseChildren=false;
}
private function redraw() {
var w:Number=10 + labelTF.width;
var h:Number=4 + labelTF.height;
this.graphics.clear();
this.graphics.beginFill(0x000000,0.4);
this.graphics.drawRoundRect(3,3,w,h,5,5);
this.graphics.moveTo(6,3 + h);
this.graphics.lineTo(12,3 + h);
this.graphics.lineTo(9,8 + h);
this.graphics.lineTo(6,3 + h);
this.graphics.endFill();
this.graphics.beginFill(0xffffff);
this.graphics.drawRoundRect(0,0,w,h,5,5);
this.graphics.moveTo(3,h);
this.graphics.lineTo(9,h);
this.graphics.lineTo(6,5 + h);
this.graphics.lineTo(3,h);
this.graphics.endFill();
}
public static function init(base:DisplayObjectContainer) {
if (instance == null) {
instance=new ToolTip ;
base.addChild(instance);
}
}
public static function register(area:DisplayObject,s:String):void {
if (instance != null) {
var prop:AccessibilityProperties=new AccessibilityProperties ;
prop.description=s;
area.accessibilityProperties=prop;
area.addEventListener(MouseEvent.MOUSE_OVER,instance.handler);
}
}
public static function unregister(area:DisplayObject):void {
if (instance != null) {
area.removeEventListener(MouseEvent.MOUSE_OVER,instance.handler);
}
}
public function show(area:DisplayObject):void {
this.area=area;
this.area.addEventListener(MouseEvent.MOUSE_OUT,this.handler);
this.area.addEventListener(MouseEvent.MOUSE_MOVE,this.handler);
labelTF.text=area.accessibilityProperties.description;
redraw();
//移到最上
this.parent.setChildIndex(instance,this.parent.numChildren - 1);
}
public function hide():void {
this.area.removeEventListener(MouseEvent.MOUSE_OUT,this.handler);
this.area.removeEventListener(MouseEvent.MOUSE_MOVE,this.handler);
this.area=null;
visible=false;
}
public function move(point:Point):void {
var lp:Point=this.parent.globalToLocal(point);
this.x=lp.x - 6;
this.y=lp.y - labelTF.height - 12;
if (! visible) {
visible=true;
}
}
private function handler(event:MouseEvent):void {
switch (event.type) {
case MouseEvent.MOUSE_OUT :
this.hide();
break;
case MouseEvent.MOUSE_MOVE :
this.move(new Point(event.stageX,event.stageY));
break;
case MouseEvent.MOUSE_OVER :
//用.target时,有时Loader不能转化为DisplayObject,用currentTarget就好了
this.show(event.currentTarget as DisplayObject);
this.move(new Point(event.stageX,event.stageY));
break;
}
}
}
}