鼠标经过延时出现Hint

舞台上有个影片剪辑mc
库中有个元件hint由bg元件和文本txt组成
import flash.events.MouseEvent;



var time:int = 500;

var dyid:int;

var flag:Boolean;



mc.addEventListener(MouseEvent.ROLL_OVER,_onRollOver);

mc.addEventListener(MouseEvent.ROLL_OUT,_onRollOut);



function _onRollOver(e:MouseEvent):void

{

	dyid = setTimeout(delayed,time,e);

}

function delayed(e:MouseEvent):void

{

	flag = true;

	Hint.addHint(e,"延时"+time+"毫秒");

}

function _onRollOut(e:MouseEvent):void

{

	if (flag)

	{

		flag = false;

		Hint.removeHint();

	}

	clearTimeout(dyid);

}

package

{

	import flash.display.DisplayObject;

	import flash.display.MovieClip;

	import flash.display.Sprite;

	import flash.display.Stage;

	import flash.events.MouseEvent;

	import flash.geom.Rectangle;

	import flash.text.TextField;

	import flash.text.TextFieldAutoSize;



	public class Hint extends Sprite

	{

		private static  var _txt:TextField;

		private static var _mcBg:MovieClip;

		private static var _hint:Hint;

		private static var _stage:Stage;

		public function Hint() 

		{

			_txt = getChildByName("txt") as TextField;

			_mcBg = getChildByName("bg") as MovieClip;

		}

		public static function getInstance():Hint

		{

			if (_hint == null)

			{

				_hint = new Hint();

			}

			return _hint;

		}

		public static function addHint(e:MouseEvent,str:String):void

		{

			_stage = e.target.stage;

			_stage.addChild(getInstance());	

			_txt.text = str;

			_txt.autoSize = TextFieldAutoSize.CENTER;

			resize();

			getInstance().x = _stage.mouseX;

			getInstance().y = _stage.mouseY;

			_stage.addEventListener(MouseEvent.MOUSE_MOVE, moveHandler);

		}

		public static function moveHandler(e:MouseEvent):void

		{

			getInstance().x = _stage.mouseX;

			getInstance().y = _stage.mouseY;

		}

		public static function resize():void

		{

			_mcBg.width = _txt.textWidth + 30;

		}

		public static function removeHint():void

		{

			_stage.removeEventListener(MouseEvent.MOUSE_MOVE, moveHandler);

			_stage.removeChild(getInstance());

		}

	}



}

你可能感兴趣的:(hint)