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;
		}
	}
}

  

 

posted on 2020-09-20 21:58  疯子_wu  阅读(275)  评论(0编辑  收藏  举报