每天学一点flash(75) ToolTip 提示
?
今晚拿了麥哥哥的程序修改,之前他給了一個(gè)ToolTip的類(lèi),在他的基礎(chǔ)上加了幾個(gè)方法這個(gè)toolTip 提示只是一個(gè)Sprite+TextField的 混搭。要是說(shuō)到其他的在玩游戲看到提示,相對(duì)來(lái)講,他們toolTip類(lèi)還是比較強(qiáng)大的。對(duì)于一些簡(jiǎn)單的提供提示的來(lái)講,還可以應(yīng)付一下。
好吧。有關(guān)toolTip類(lèi)的制作,涉及就是Sprite+TextField的混搭。需要文本,需要圖形的輔助。這樣看起來(lái)就有摸有樣了。網(wǎng)上已經(jīng)有更加強(qiáng)大的toolTip類(lèi)庫(kù)了。資源可以隨意發(fā)揮。
使用說(shuō)明:
執(zhí)行的時(shí)候 在場(chǎng)景里面隨便添加兩個(gè)元件進(jìn)行測(cè)試。
首先執(zhí)行ToolTipManager.init(this); 讓主場(chǎng)景將toolTip的添加到顯示列表當(dāng)中。
ToolTipManager里面有幾個(gè)靜態(tài)方法,只是需要進(jìn)行提示的信息。使用的辦法是。
ToolTipManager.addToolTip(mcA,str,10,10);
mcA是舞臺(tái)上的元件,str是要顯示的內(nèi)容,10 和10是偏移鼠標(biāo)相對(duì)位置。
如果需要對(duì)鼠標(biāo)監(jiān)聽(tīng)進(jìn)行刪除信息。可以執(zhí)行第二種的方法。
ToolTipManager.addToolTipByName("mcB",mcB,str2);
ToolTipManager.removeListenerByName("mcB");
如果鼠標(biāo)事件不想封裝在里面,可以使用ToolTipManager.show() 替代,后面有幾個(gè)方法是在一個(gè)網(wǎng)友麥哥哥基礎(chǔ)上修改的。
使用的方法很容易,基本上適應(yīng)一部分需求,但是對(duì)于更加強(qiáng)的信息提示,這樣這個(gè)ToolTip就需要重新修改。改變以適應(yīng)更多不同需求。
缺點(diǎn)
講到缺點(diǎn)。有一些方法不能很有效針對(duì)全部情況。會(huì)存在一些設(shè)計(jì)的問(wèn)題。
大致上就當(dāng)介紹使用。
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="等級(jí):1"+"\r"+ "性別:男"+"\r"+ "經(jīng)驗(yàn):29"+"\r"+ "勝率:37.5%"+"\r"+ "稱(chēng)號(hào):無(wú)"+"\r"+ "文功:平民"+"\r"+ "功勛:平民"+"\r"+ "勝 3 輸 5 逃 0"; var str2:String="等級(jí):3"+"\r"+ "性別:男"+"\r"+ "經(jīng)驗(yàn):50"+"\r"+ "勝率:36.5%"+"\r"+ "稱(chēng)號(hào):無(wú)"+"\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); } } //移動(dòng)位置 public static function move(x:Number,y:Number):void { toolTip.move(x,y); } //獲取toolTip對(duì)象 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; } } //刪除監(jiān)聽(tīng) 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(); } //設(shè)置和獲取底部顏色 public function set color(value:uint):void { _color=value; } public function get color():uint { return _color; } //獲取textField對(duì)象 public function get textField():TextField { return _textField; } //設(shè)置文本提示 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; } //設(shè)置位置 public function move(x:Number,y:Number):void { this.x=x; this.y=y; } //設(shè)置濾鏡 public function setTextFilters(...arg):void { _textField.filters=arg; } //去除濾鏡 public function delTextFilters():void { _textField.filters=null; } } }
package { public interface IToolTip { //設(shè)置和獲取底部顏色 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():*; } }
?倘若再進(jìn)行修改的話(huà).讓提示帶一顏色的。可以設(shè)置Html
var str3:String="<font color='#FFFF00' size='12'><b>孫權(quán)</b></font> 吳 體力4\r"+ "<font color='#00ff00' size='12'><b>制衡</b></font>:\r出牌階段,你可以棄掉任意數(shù)量的牌,然后摸取等量的牌\r每回合限用一次\r"+ "<font color='#00ff00' size='12'><b>救援</b></font>:\r主公技,鎖定技,其他吳勢(shì)力角色在你瀕死狀態(tài)下對(duì)你\r使用【桃】時(shí),你額外回復(fù)1點(diǎn)體力。";
讓textField 以html方式進(jìn)行顯示。
所以可以對(duì)ToolTipManager,加以改造。
在測(cè)試的時(shí)候,在播放器顯示兩種文本設(shè)置方式的時(shí)候,混在一起使用發(fā)現(xiàn)有一些不正常 在網(wǎng)頁(yè)瀏覽器則顯示正常。
設(shè)置text 和htmltext 方式的時(shí)候 遇到一個(gè)這樣的問(wèn)題。不清楚發(fā)生了什么事
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); } } //移動(dòng)位置 public static function move(x:Number,y:Number):void { toolTip.move(x,y); } //獲取toolTip對(duì)象 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; } } //刪除監(jiān)聽(tīng) 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); } } }
總結(jié)
以上是生活随笔為你收集整理的每天学一点flash(75) ToolTip 提示的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 阿里纳斯Adidas广告词
- 下一篇: 开发部考核管理制度 随想