每天学一点flash(75) ToolTip 提示

 

今晚拿了麦哥哥的程序修改,之前他给了一个ToolTip的类,在他的基础上加了几个方法这个toolTip 提示只是一个Sprite+TextField的 混搭。要是说到其他的在玩游戏看到提示,相对来讲,他们toolTip类还是比较强大的。对于一些简单的提供提示的来讲,还可以应付一下。

好吧。有关toolTip类的制作,涉及就是Sprite+TextField的混搭。需要文本,需要图形的辅助。这样看起来就有摸有样了。网上已经有更加强大的toolTip类库了。资源可以随意发挥。

使用说明:

执行的时候 在场景里面随便添加两个元件进行测试。

首先执行ToolTipManager.init(this); 让主场景将toolTip的添加到显示列表当中。

ToolTipManager里面有几个静态方法,只是需要进行提示的信息。使用的办法是。

ToolTipManager.addToolTip(mcA,str,10,10);

mcA是舞台上的元件,str是要显示的内容,10 和10是偏移鼠标相对位置。

如果需要对鼠标监听进行删除信息。可以执行第二种的方法。

ToolTipManager.addToolTipByName("mcB",mcB,str2);

ToolTipManager.removeListenerByName("mcB");

如果鼠标事件不想封装在里面,可以使用ToolTipManager.show() 替代,后面有几个方法是在一个网友麦哥哥基础上修改的。

使用的方法很容易,基本上适应一部分需求,但是对于更加强的信息提示,这样这个ToolTip就需要重新修改。改变以适应更多不同需求。

缺点

讲到缺点。有一些方法不能很有效针对全部情况。会存在一些设计的问题。

大致上就当介绍使用。

package { import flash.display.*; import flash.events.*; import flash.filters.GlowFilter; public class Main extends Sprite { public function Main() { ToolTipManager.init(this); init(); } private function init():void { var str:String="等级:1"+"\r"+ "性别:男"+"\r"+ "经验:29"+"\r"+ "胜率:37.5%"+"\r"+ "称号:无"+"\r"+ "文功:平民"+"\r"+ "功勋:平民"+"\r"+ "胜 3 输 5 逃 0"; var str2:String="等级:3"+"\r"+ "性别:男"+"\r"+ "经验:50"+"\r"+ "胜率:36.5%"+"\r"+ "称号:无"+"\r"+ "文功:平民"+"\r"+ "功勋:平民"+"\r"+ "胜 10 输40 逃 0"; ToolTipManager.getToolTip.color=0x353128;//底部颜色 ToolTipManager.getToolTip.textField.textColor=0xD7D1B7;//文本颜色 ToolTipManager.getToolTip.setTextFilters(new GlowFilter(0x0)); ToolTipManager.addToolTip(mcA,str,10,10); ToolTipManager.addToolTipByName("mcB",mcB,str2); stage.addEventListener(MouseEvent.CLICK,onClick); function onClick(event:MouseEvent):void { ToolTipManager.removeListenerByName("mcB"); } } } }

package { import flash.display.DisplayObjectContainer; import flash.events.*; import flash.utils.Dictionary; public class ToolTipManager { private static var container:DisplayObjectContainer; private static var toolTip:ToolTip=null; private static var data:Dictionary=new Dictionary(); public function ToolTipManager() { } //初始化信息 public static function init(container:DisplayObjectContainer):void { ToolTipManager.container=container; if (toolTip==null) { toolTip=new ToolTip(); } } //显示信息 public static function show(str:String,offX:Number=0,offY:Number=0):void { if (!container)return; toolTip.text=str; toolTip.x=container.stage.mouseX+offX; toolTip.y=container.stage.mouseY+offY; container.addChild(toolTip); } //隐藏显示 public static function hide():void { if (!container)return; toolTip.text=""; if (container.contains(toolTip)) { container.removeChild(toolTip); } } //移动位置 public static function move(x:Number,y:Number):void { toolTip.move(x,y); } //获取toolTip对象 public static function get getToolTip():ToolTip { return toolTip; } //添加提示信息 public static function addToolTip(obj:*,str:String,offX:Number=0,offY:Number=0):void { if (!obj.hasEventListener(MouseEvent.MOUSE_OVER)||!obj.hasEventListener(MouseEvent.MOUSE_OUT)) { obj.addEventListener(MouseEvent.MOUSE_OVER,onMouseOverHandler); obj.addEventListener(MouseEvent.MOUSE_OUT,onMouseOutHandler); obj.addEventListener(MouseEvent.MOUSE_MOVE,onMouseMoveHandler); function onMouseOverHandler(event:MouseEvent):void { ToolTipManager.show(str,offX,offY); } function onMouseOutHandler(event:MouseEvent):void { ToolTipManager.hide(); } function onMouseMoveHandler(event:MouseEvent):void { ToolTipManager.move(event.stageX+offX,event.stageY+offY); } } } //添加提示信息 public static function addToolTipByName(id:String,obj:*,str:String,offX:Number=0,offY:Number=0):void { if (!obj.hasEventListener(MouseEvent.MOUSE_OVER)||!obj.hasEventListener(MouseEvent.MOUSE_OUT)) { obj.addEventListener(MouseEvent.MOUSE_OVER,onMouseOverHandler); obj.addEventListener(MouseEvent.MOUSE_OUT,onMouseOutHandler); obj.addEventListener(MouseEvent.MOUSE_MOVE,onMouseMoveHandler); function onMouseOverHandler(event:MouseEvent):void { ToolTipManager.show(str,offX,offY); } function onMouseOutHandler(event:MouseEvent):void { ToolTipManager.hide(); } function onMouseMoveHandler(event:MouseEvent):void { ToolTipManager.move(event.stageX+offX,event.stageY+offY); } var funs:Object={obj:obj,mouse_over:onMouseOverHandler,mouse_out:onMouseOutHandler,mouse_move:onMouseMoveHandler}; data[id]=funs; } } //删除监听 public static function removeListenerByName(id:String):void { if(data[id]==null)return; data[id].obj.removeEventListener(MouseEvent.MOUSE_OVER,data[id].mouse_over); data[id].obj.removeEventListener(MouseEvent.MOUSE_OUT,data[id].mouse_out); data[id].obj.removeEventListener(MouseEvent.MOUSE_MOVE,data[id].mouse_move); } } }

package { import flash.display.Sprite; import flash.events.*; import flash.text.* import flash.geom.*; public class ToolTip extends Sprite implements IToolTip { private var _textField:TextField;//提示文本 private var _color:uint=0xffffe1;//颜色 public function ToolTip() { init(); } private function init():void { _textField=new TextField(); _textField.selectable=false; _textField.mouseEnabled=false; _textField.defaultTextFormat=new TextFormat("Arial",12); _textField.autoSize=TextFieldAutoSize.LEFT; addChild(_textField); } private function redraw():void { addEventListener(Event.ENTER_FRAME,onReDrawHandler); } private function onReDrawHandler(event:Event):void { removeEventListener(Event.ENTER_FRAME,onReDrawHandler); draw(); } private function draw():void { _textField.x=4; _textField.y=4; var width:Number=_textField.textWidth; var height:Number=_textField.textHeight; this.graphics.clear(); //this.graphics.lineStyle(0,1); this.graphics.beginFill(color); this.graphics.drawRoundRect(0,0,width+8,height+8,4,4); this.graphics.endFill(); } public function clone():* { return new ToolTip(); } //设置和获取底部颜色 public function set color(value:uint):void { _color=value; } public function get color():uint { return _color; } //获取textField对象 public function get textField():TextField { return _textField; } //设置文本提示 public function set text(str:String):void { _textField.text=str; redraw(); } public function get text():String { return _textField.text; } public function set htmlText(str:String):void { _textField.htmlText=str; redraw(); } public function get htmlText():String { return _textField.htmlText; } //设置位置 public function move(x:Number,y:Number):void { this.x=x; this.y=y; } //设置滤镜 public function setTextFilters(...arg):void { _textField.filters=arg; } //去除滤镜 public function delTextFilters():void { _textField.filters=null; } } }

package { public interface IToolTip { //设置和获取底部颜色 function set color(value:uint):void; function get color():uint; function set text(str:String):void; function get text():String; function set htmlText(str:String):void; function get htmlText():String; function move(x:Number,y:Number):void; function clone():*; } }

 倘若再进行修改的话.让提示带一颜色的。可以设置Html

var str3:String="<font color='#FFFF00' size='12'><b>孙权</b></font> 吴 体力4\r"+ "<font color='#00ff00' size='12'><b>制衡</b></font>:\r出牌阶段,你可以弃掉任意数量的牌,然后摸取等量的牌\r每回合限用一次\r"+ "<font color='#00ff00' size='12'><b>救援</b></font>:\r主公技,锁定技,其他吴势力角色在你濒死状态下对你\r使用【桃】时,你额外回复1点体力。";

让textField 以html方式进行显示。

所以可以对ToolTipManager,加以改造。

在测试的时候,在播放器显示两种文本设置方式的时候,混在一起使用发现有一些不正常 在网页浏览器则显示正常。

设置text 和htmltext 方式的时候 遇到一个这样的问题。不清楚发生了什么事

package { import flash.display.DisplayObjectContainer; import flash.events.*; import flash.utils.Dictionary; public class ToolTipManager { private static var container:DisplayObjectContainer; private static var toolTip:ToolTip=null; private static var data:Dictionary=new Dictionary(); private static var typeData:Dictionary=new Dictionary(); public static const HTML:String="html"; public static const TEXT:String="text"; private static var type:String="text"; public function ToolTipManager() { } //初始化信息 public static function init(container:DisplayObjectContainer):void { ToolTipManager.container=container; if (toolTip==null) { toolTip=new ToolTip(); } } //显示信息 public static function show(str:String,offX:Number=0,offY:Number=0,type:String="text"):void { if (!container)return; if(type=="text") { toolTip.text=str; } else if(type=="html") { toolTip.htmlText=str; } toolTip.x=container.stage.mouseX+offX; toolTip.y=container.stage.mouseY+offY; container.addChild(toolTip); } //隐藏显示 public static function hide():void { if (!container)return; toolTip.text=""; toolTip.htmlText=""; if (container.contains(toolTip)) { container.removeChild(toolTip); } } //移动位置 public static function move(x:Number,y:Number):void { toolTip.move(x,y); } //获取toolTip对象 public static function get getToolTip():ToolTip { return toolTip; } //添加提示信息 public static function addToolTip(obj:*,str:String,offX:Number=0,offY:Number=0,type:String="text"):void { if (!obj.hasEventListener(MouseEvent.MOUSE_OVER)||!obj.hasEventListener(MouseEvent.MOUSE_OUT)) { obj.addEventListener(MouseEvent.MOUSE_OVER,onMouseOverHandler); obj.addEventListener(MouseEvent.MOUSE_OUT,onMouseOutHandler); obj.addEventListener(MouseEvent.MOUSE_MOVE,onMouseMoveHandler); function onMouseOverHandler(event:MouseEvent):void { ToolTipManager.show(str,offX,offY,type); } function onMouseOutHandler(event:MouseEvent):void { ToolTipManager.hide(); } function onMouseMoveHandler(event:MouseEvent):void { ToolTipManager.move(event.stageX+offX,event.stageY+offY); } } } //添加提示信息 public static function addToolTipByName(id:String,obj:*,str:String,offX:Number=0,offY:Number=0,type:String="text"):void { if (!obj.hasEventListener(MouseEvent.MOUSE_OVER)||!obj.hasEventListener(MouseEvent.MOUSE_OUT)) { obj.addEventListener(MouseEvent.MOUSE_OVER,onMouseOverHandler); obj.addEventListener(MouseEvent.MOUSE_OUT,onMouseOutHandler); obj.addEventListener(MouseEvent.MOUSE_MOVE,onMouseMoveHandler); function onMouseOverHandler(event:MouseEvent):void { ToolTipManager.show(str,offX,offY,type); } function onMouseOutHandler(event:MouseEvent):void { ToolTipManager.hide(); } function onMouseMoveHandler(event:MouseEvent):void { ToolTipManager.move(event.stageX+offX,event.stageY+offY); } var funs:Object={obj:obj,mouse_over:onMouseOverHandler,mouse_out:onMouseOutHandler,mouse_move:onMouseMoveHandler}; data[id]=funs; } } //删除监听 public static function removeListenerByName(id:String):void { if(data[id]==null)return; data[id].obj.removeEventListener(MouseEvent.MOUSE_OVER,data[id].mouse_over); data[id].obj.removeEventListener(MouseEvent.MOUSE_OUT,data[id].mouse_out); data[id].obj.removeEventListener(MouseEvent.MOUSE_MOVE,data[id].mouse_move); } } }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值