AS3 CookBook学习整理(八)
1. AS3的事件機制
事件流機制即為捕獲--目標--冒泡,分別對應event.eventPhase的值1(EventPhase.CAPTURING_PHASE),2(EventPhase.AT_TARGET),3(EventPhase.BUBBLING_PHASE)
假設有3個Sprite,分別為綠、藍、黃(如圖),層疊關系為綠色包含藍色,藍色包含黃色
情況一:最頂層的黃色有一個點擊事件,另外兩個沒有點擊事件
結果:target是--黃色方塊,currentTarget是--黃色方塊,階段是--2
情況二:三個Sprite都有一個點擊事件,然后點擊最上層的黃色方塊
結果:
target是--黃色方塊,currentTarget是--黃色方塊,階段是--2
target是--黃色方塊,currentTarget是--藍色方塊,階段是--3
target是--黃色方塊,currentTarget是--綠色方塊,階段是--3
可以看出來,event的target始終是指單擊的目標,屬于目標階段;而currentTarget指向處理事件(即活動)的目標,屬于冒泡階段,currentTarget屬性應具備兩個條件,一是它注冊了偵聽器,二是正在處理事件
{flash.display.Sprite;flash.events.MouseEvent;flash.text.TextField;flash.text.TextFieldAutoSize;Sample0413Sprite{lblInfo:TextField;Sample0413(){greenRect:Sprite = Sprite();greenRect.graphics.beginFill(0x00FF00);greenRect.graphics.drawRect(100,100,200,200);greenRect.graphics.endFill();greenRect.addEventListener(MouseEvent.CLICK,onGreenClick);greenRect.name = ;.addChild(greenRect);blueRect:Sprite = Sprite();blueRect.graphics.beginFill(0x0000FF);blueRect.graphics.drawRect(130,130,150,150);blueRect.graphics.endFill();blueRect.addEventListener(MouseEvent.CLICK,onBlueClick);blueRect.name = ;greenRect.addChild(blueRect);yellowRect:Sprite = Sprite();yellowRect.graphics.beginFill(0xFFFF00);yellowRect.graphics.drawRect(160,160,100,100);yellowRect.graphics.endFill();yellowRect.addEventListener(MouseEvent.CLICK,onYellowClick);yellowRect.name = ;blueRect.addChild(yellowRect);lblInfo = TextField();lblInfo.background = ;lblInfo.x = 10;lblInfo.y = 20;lblInfo.width = 400;.addChild(lblInfo);}onGreenClick(event:MouseEvent):{.lblInfo.appendText();}onBlueClick(event:MouseEvent):{.lblInfo.appendText();}onYellowClick(event:MouseEvent):{.lblInfo.appendText();}onClick(event:MouseEvent):{targetStr:String = + (event.target Sprite).name;curTargetStr:String = + (event.currentTarget Sprite).name;phaseStr:String = + event.eventPhase;.lblInfo.appendText(targetStr + + curTargetStr + + phaseStr + );}} }2. FocusEvent焦點事件
一共有4個焦點事件:FocusIn、FocusOut、KEY_FOCUS_CHANGE、MOUSE_FOCUS_CHANGE
當焦點改變時focusIn和focusOut 事件會同時激活,它們屬于不可取消的事件
keyFocusChange和mouseFocusChange是可取消的事件,可以調用FocusEvent.preventDefault( )方法取消默認事件
FocusEvent有一個relatedObject屬性,對于focusIn而言,relatedObject屬性是剛才擁有焦點的對象的;對于其它三個事件,relatedObject屬性是現在接收到焦點的對象
{flash.display.Sprite;flash.events.FocusEvent;flash.text.TextField;flash.text.TextFieldAutoSize;flash.text.TextFieldType;Sample0414Sprite{lblInfo:TextField;Sample0414(){textBox:TextField = TextField();textBox.name = ;textBox.type = TextFieldType.INPUT;textBox.background = ;textBox.width = 200;textBox.height = 20;textBox.addEventListener(FocusEvent.KEY_FOCUS_CHANGE,onKeyFocusChange);.addChild(textBox);textBox = TextField();textBox.name = ;textBox.type = TextFieldType.INPUT;textBox.background = ;textBox.width = 200;textBox.height = 20;textBox.y = 80;textBox.addEventListener(FocusEvent.KEY_FOCUS_CHANGE,onKeyFocusChange);.addChild(textBox);lblInfo = TextField();lblInfo.y = 200;lblInfo.autoSize = TextFieldAutoSize.CENTER;.addChild(lblInfo);}onFocusIn(event:FocusEvent):{txt:TextField = event.target TextField;txt.text = txt.name;(event.relatedObject!=null){lblInfo.text = + (event.relatedObject TextField).name;}}onFocusOut(event:FocusEvent):{(event.relatedObject!=null){lblInfo.text = + (event.relatedObject TextField).name;}}onKeyFocusChange(event:FocusEvent):{txt:TextField = event.target TextField;(txt.text == ) {event.preventDefault();} (event.relatedObject!=null){strRelatedObj:String = (event.relatedObject? ? TextField).name;lblInfo.text = + strRelatedObj;}}} }
3. 監聽用戶輸入的文本
通過TextEvent事件監聽用戶對文本框內容的修改,如刪除,剪切,插入或者拷貝等操作,對文本框的每一次修改都會激活textInput事件,可以通過event.preventDefault()取消顯示輸入的文本
{flash.display.Sprite;flash.events.TextEvent;flash.text.TextField;flash.text.TextFieldType;Sample0414Sprite{lblInfo:TextField;Sample0414(){textBox:TextField = TextField();textBox.type = TextFieldType.INPUT;textBox.background = ;textBox.width = 200;textBox.height = 20;textBox.addEventListener(TextEvent.TEXT_INPUT,onTextInput);.addChild(textBox);}onTextInput(event:TextEvent):{(event.text.toLowerCase().indexOf()>-1){event.preventDefault();}}} }4. 為TextField添加超鏈接
同設置樣式一樣,也有三種方式實現:直接加入標記、設置TextFormatter、設置CSS
設置TextFormatter:
textBox:TextField = TextField(); textBox.text = ; formatter:TextFormat = TextFormat(); formatter.color = 0xFF0000; formatter.url = ; formatter.target = ; textBox.setTextFormat(formatter); .addChild(textBox);設置CSS:
textBox:TextField = TextField(); textBox.htmlText = ;css:StyleSheet = StyleSheet( ); css.parseCSS(); textBox.styleSheet = css;.addChild(textBox);5. 用超鏈接調用ActionScript代碼
在一個鏈接地址前加上event:,即可以響應TextEvent.LINK事件。TextEvent.text屬性就是url的地址
{flash.display.Sprite;flash.events.TextEvent;flash.text.TextField;flash.text.TextFormat;Sample0414Sprite{Sample0414(){textBox:TextField = TextField();textBox.addEventListener(TextEvent.LINK,onClickLink);textBox.htmlText = ;formatter:TextFormat = TextFormat();formatter.color = 0xFF0000;formatter.url = ;formatter.target = ;textBox.setTextFormat(formatter);textBox.addEventListener(TextEvent.LINK, onClickLink);.addChild(textBox);}onClickLink(event:TextEvent):{(event.text);}} }6. 高級文本布局
TextField 類定義了一系列API用于精確控制文本布局:
getCharIndexAtPoint(x,y) -- 得到坐標(x,y)下的字符
getCharBoundaries(index) -- 得到索引index對應字符的Rectangle
numLines屬性 -- 得到TextField的文本行數
getLineIndexAtPoint(x,y) -- 得到坐標(x,y)下對應的行索引
getLineIndexOfChar(index) -- 得到索引index對應字符的行索引
getLineLength(lineIndex) -- 得到索引lineIndex對應行的字符數
getLineText(lineIndex) -- 得到索引lineIndex對應行的文本
getLineOffset(lineIndex) -- 得到索引lineIndex對應行的第一個字符的索引
getLineMetrics(lineIndex) -- 得到索引lineIndex對應行的度量信息,包括x,width,leading,height,descent,ascent相關屬性
getFirstCharInParagraph(index) -- 如果給定一個字符索引index,則返回同一段落中第一個字符的索引
getParagraphLength(index) -- 如果給定一個字符索引,則返回包含該字符的段落的長度
{flash.display.Sprite;flash.events.MouseEvent;flash.text.TextField;flash.text.TextFieldType;Sample0421Sprite{label:TextField;Sample0421(){textBox:TextField = TextField();textBox.type = TextFieldType.INPUT;textBox.multiline = ;textBox.htmlText = ;textBox.addEventListener(MouseEvent.CLICK,onClick);textBox.background = ;textBox.wordWrap = ;.addChild(textBox);label = TextField();label.background = ;label.x = 150;label.width = 250;label.height = 300;.addChild(label);}onClick(event:MouseEvent):{txt:TextField = event.target TextField;label.text = ;{label.appendText(+txt.numLines+);label.appendText(+txt.getLineIndexAtPoint(mouseX,mouseY)+);label.appendText(+
txt.getLineIndexOfChar(20)+);label.appendText(+txt.getLineLength(2)+);label.appendText(+txt.getLineText(2)+);label.appendText(+txt.getLineOffset(2)+);label.appendText(+txt.getLineMetrics(2).width+);label.appendText(+
txt.getFirstCharInParagraph(5)+);label.appendText(+
txt.getParagraphLength(3)+);(txt.text.charAt(5));}(ex:Error){label.text = ex.message;}}} }
7. 高級抗鋸齒
對于嵌入字體可通過設置文本框的antiAliasType屬性為flash.text.AntiAliasType.ADVANCED,然后設置gridTypeFit和sharpness屬性
flash.text.AntiAliasType.NORMAL -- 應用常規文本消除鋸齒功能。 這與 Flash Player 7 和更早版本中使用的消除鋸齒類型匹配。
應用高級消除鋸齒功能,這增加了文本的可讀性。 (此功能在 Flash Player 8 中可用。) 高級消除鋸齒功能可以高品質呈現小尺寸的字體。 它最適合在具有大量小字號文本的應用程序中使用。 建議不要對大于 48 磅的字體使用高級消除鋸齒功能
gridFitType屬性用于此文本字段的網格固定類型,僅在文本字段的 flash.text.AntiAliasType 屬性設置為 flash.text.AntiAliasType.ADVANCED 時才應用此屬性
flash.text.GridFitType.NONE -- 指定無網格固定。 不強制根據像素網格調整字型中的水平線和垂直線。 此設置通常適合動畫或大號字。
flash.text.GridFitType.PIXEL -- 指定粗水平線和垂直線適合像素網格。 此設置僅適用于左對齊文本字段。 若要使用此設置,文本字段的 flash.dispaly.AntiAliasType 屬性必須設置為 flash.text.AntiAliasType.ADVANCED。 此設置通常能為左對齊文本提供最佳可讀性。
flash.text.GridFitType.SUBPIXEL -- 指定粗水平線和垂直線適合 LCD 顯示器上的子像素網格。 若要使用此設置,文本字段的 flash.text.AntiAliasType 屬性必須設置為 flash.text.AntiAliasType.ADVANCED。 flash.text.GridFitType.SUBPIXEL 設置通常適合右對齊或居中的動態文本,有時,為了在動畫與文本品質之間達到一種平衡,也可使用此設置。
sharpness 屬性范圍在-400 到400,默認為0 ,它決定字體外框的清晰程度。值越低越模糊,越高越銳利。當文字比較模糊時可以設置gridFitType 為PIXEL 或SUBPIXEL 以提高清晰度
8. 替換文本
使用replaceSelectedText(newText)方法替換選中的文字或用replaceText(startIndex,endIndex,newText)方法替換某一范圍的文字
{flash.display.Sprite;flash.events.KeyboardEvent;flash.text.TextField;flash.text.TextFieldType;Sample0422Sprite{Sample0422(){textBox:TextField = TextField();textBox.type = TextFieldType.INPUT;textBox.background = ;.addEventListener(KeyboardEvent.KEY_DOWN,onKeyDown);.addChild(textBox);}onKeyDown(event:KeyboardEvent):{(event.keyCode == 13){txt:TextField = event.target TextField;txt.replaceSelectedText();txt.replaceText(0,2,);}}} }9. 獲取可用字體列表
Font.enumerateFonts(bool)方法返回可用字體列表,參數默認為false,將返回一個只包括嵌入字體的列表;為true則返回一個包括所有字體(設備字體和嵌入字體)的列表
{flash.display.Sprite;flash.text.Font;[Embed(source=,fontName=,mimeType=)]Sample0422Sprite{Sample0422(){allFonts:Array = Font.enumerateFonts();?allFonts.sortOn(, Array.CASEINSENSITIVE);for (i:uint=0; i<allFonts.length; i++){ (allFonts[i].fontName);} }} }10. 改變顏色
先獲取可視化對象的obj.transform.colorTransform對象,然后修改該對象,再重新賦值給對象
設置顏色,可以直接設置ColorTransform.color屬性,也可以分開設置redOffset,greenOffset,blueOffset和alphaOffset;取值范圍是-255到255(alpha 值范圍在0 到100,與alphaOffset是不同的)
{flash.display.Shape;flash.display.Sprite;flash.geom.ColorTransform;Sample0422Sprite{Sample0422(){rect:Shape = Shape();rect.graphics.beginFill(0xFF0000);rect.graphics.drawRect(100,100,150,100);rect.graphics.endFill();.addChild(rect);ct:ColorTransform = rect.transform.colorTransform;ct.redOffset = 128;ct.greenOffset = 128;ct.blueOffset = 128;ct.alphaOffset = 50;rect.transform.colorTransform = ct;}} }?
轉載于:https://www.cnblogs.com/CoderWayne/archive/2010/07/15/1778065.html
總結
以上是生活随笔為你收集整理的AS3 CookBook学习整理(八)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Python GUI案例之看图猜成语开发
- 下一篇: 中国城市统计年鉴中地级市面板数据(200