AS3迴圈滾動文字
阿新 • • 發佈:2020-09-21
AS3迴圈滾動文字
/** * * *-------------------* * | *** 滾動文字 *** | * *-------------------* * * 編輯修改收錄:fengzi(瘋子、wu341、wgq341) * * 不會寫程式碼,我是程式碼搬運工。 * * 聯絡方式:QQ(493712833)。 * * 隨 筆: https://www.cnblogs.com/fengziwu/ * * 版權協議:請自覺遵守LGPL協議,歡迎修改、複製、轉載、傳播給更多需要的人。 * 免責宣告:任何因使用此軟體導致的糾紛與軟體/程式開發者無關。 * 日 期: 2014.02.08 * * * ------------------ Example -------------------- * * import fengzi.txtString.RollTxt; var txt:RollTxt=new RollTxt(150,true,15,14,0xFFFFFF,1) txt.setTxt("五十一各種往事中國可憐見解放環境開會交換機後交換機開會無理數難道",true) addChild(txt) */ package fengzi.txtString { import flash.display.GradientType; import flash.display.Sprite; import flash.events.Event; import flash.geom.Matrix; import flash.text.TextField; import flash.text.TextFieldAutoSize; import flash.text.TextFormat; import flash.events.MouseEvent; public class RollTxt extends Sprite { protected var txtW:int; protected var txt:TextField; protected var txtInfo:Object; private var txtFormat:TextFormat; private var txtMask:Sprite; private var txtHGap:int = 8; private var isGradient:Boolean = false; private var gradientDistance:int = 10; private var colors:Array = []; private var alphas:Array = []; private var ratios:Array = []; private var tempOffset:int = 38; private var matrix:Matrix; private var _speed:Number//滾動速度 private var speed:Number /** * * @param txtW 滾動文字的可視區域寬度 * @param isGradient 是否有漸變遮罩 * @param gradientDistance 漸變遮罩漸變色距離 (可以將.mask註釋掉,檢視效果) * @param size 滾動文字大小 * @param color 滾動文字顏色 * @param kerning 字間距,預設不調整(調整請指定間距大於0) * @param font 滾動文字字型 * @param bold 滾動文字是否加粗 * */ public function RollTxt(txtW:int , isGradient:Boolean , gradientDistance:int = 10, size:int = 12 , color:int = 0xffffff ,kerning:uint=0, font:String = "Verdana",bold:Boolean=false) { this.txtW = txtW; this.isGradient = isGradient; this.gradientDistance = gradientDistance; this.gradientDistance = ((this.gradientDistance * 2 + tempOffset) >255)?(int((255 -tempOffset)/2)):this.gradientDistance this.tempOffset = ((this.gradientDistance * 2 + tempOffset) >255)?(255 - this.gradientDistance * 2 -10):this.tempOffset; if(isGradient) { colors = [0xffffff,0xffffff,0xffffff,0xffffff]; alphas = [0,100,100,0]; matrix = new Matrix(); ratios = [0 , gradientDistance , 255 - gradientDistance - tempOffset , 255]; } txtFormat = new TextFormat(); txtFormat.font = font; txtFormat.size = size; txtFormat.color = color; txtFormat.bold = bold; if (kerning>0) { txtFormat.kerning = true; txtFormat.letterSpacing = kerning; } txt = new TextField(); txt.autoSize = TextFieldAutoSize.LEFT; txt.multiline = false; txt.wordWrap = false; txt.selectable = false; txt.mouseEnabled = txt.mouseWheelEnabled = false; addChild(txt); txt.defaultTextFormat = txtFormat; txtMask = new Sprite(); addChild(txtMask); txt.mask = txtMask; txtInfo = {}; } /** * 例項RollTxt,呼叫setTxt開始滾動文字 * @param value 滾動的文字 * @param _speed 滾動速度 * @param isMouse 滑鼠移入時是否停止滾動 * */ public function setTxt(value:String,_speed:Number=0.5,isMouse:Boolean=true):void { this._speed=_speed speed=_speed if(hasEventListener(Event.ENTER_FRAME)) { removeEventListener(Event.ENTER_FRAME , enterHandler); txt.x = 0; } txtInfo.txtNum = 0; txtInfo.txtSingleW = null; txt.text = value; txtInfo.txtSingleW = txt.textWidth; txt.text = ""; var tempW:int = (txtInfo.txtSingleW > txtW )?txtInfo.txtSingleW:txtW; while(txt.textWidth < tempW * 2) { txt.appendText(value); txtInfo.txtNum ++; } creatMask(); txtInfo.txtNum = Math.floor(txtInfo.txtNum/2); txtInfo.endPosition = - txtInfo.txtNum * txtInfo.txtSingleW; if(isMouse){ this.addEventListener(MouseEvent.MOUSE_OVER,onOverHandler) } start(); } private function onOverHandler(event:MouseEvent):void{ speed=0 this.addEventListener(MouseEvent.MOUSE_OUT,onOutHandler) } private function onOutHandler(event:MouseEvent):void{ speed=_speed txt.removeEventListener(MouseEvent.MOUSE_OUT,onOutHandler) } private function creatMask():void { if(isGradient) { matrix.createGradientBox(txtW, (txt.textHeight + txtHGap), Math.PI/1, 0, 0 ); txtMask.graphics.beginGradientFill(GradientType.LINEAR, colors, alphas, ratios, matrix ); txtMask.graphics.drawRect(0, -txtHGap/2, txtW, (txt.textHeight + txtHGap)); txtMask.graphics.endFill(); txtMask.cacheAsBitmap = true; txt.cacheAsBitmap = true; } else { txtMask.graphics.clear(); txtMask.graphics.beginFill(0x00ffff,0.45); txtMask.graphics.drawRect(0,-txtHGap/2,txtW,(txt.textHeight + txtHGap)); txtMask.graphics.endFill(); } } protected function enterHandler(e:Event):void { txt.x -=speed; if(txt.x <= txtInfo.endPosition) { txt.x = 0; } } private function start():void { addEventListener(Event.ENTER_FRAME , enterHandler); } public override function get width():Number { return txtW; } public override function get height():Number { return txt.textHeight + txtHGap; } } }