自己写的一个字幕滚动类(方便调用)

package {
import flash.display.DisplayObjectContainer;
import flash.display.Shape;
import flash.display.Sprite;
import flash.events.MouseEvent;
import flash.events.TimerEvent;
import flash.text.TextField;
import flash.text.TextFieldAutoSize;
import flash.text.TextFormat;
import flash.utils.Timer;
/****
* @by zou
* 2008-12-23
* ******/
public class TestProject extends Sprite
{
   //滚动的文本
   private var _textField : TextField = new TextField();
   //文本格式的控制
   private var _textFormat : TextFormat = new TextFormat();
   //定时器初始化
   private var _timer : Timer;
   //储存文本的容器
   private var _sprite : Sprite = new Sprite();
   //遮罩
   private var _shape :Shape = new Shape();
   public function TestProject(time : uint=40)
   {
//    var rollText : RollText = new RollText();
//    addChild(rollText);
   var _sprite1 : Sprite = new Sprite();
  

    init(time);
    mangerListener();
   
    displayText(_sprite1,100,100,"梦境世界欢迎您的到来","H",100,100);
   
    addChild(_sprite1);
   
   }
   //代码集中控制
   private function init(time : uint) : void{
    _timer = new Timer(time);
    _textField.mask=_shape ;
   }
   //监听集中管理
   private function mangerListener():void
   {
    _textField.addEventListener(MouseEvent.MOUSE_OVER,_textField_OVER);
    _textField.addEventListener(MouseEvent.MOUSE_OUT,_textField_OUT);
   }
   //鼠标滚动事件
   private function _textField_OVER(event : MouseEvent) : void{
    _timer.stop();
   }
   //鼠标滚过事件
   private function _textField_OUT(event : MouseEvent) : void{
    _timer.start();
   }
   //父类容器,滚动类型
   public function displayText(container : DisplayObjectContainer,
          width :uint,height : uint,text : String,type : String="H",
          x:Number=0,y:Number=0) : void{
     //文本的格式控制     
     _textField.defaultTextFormat=_textFormat;
     //将_sprite添加到父容器中
     container.addChild(_sprite);
     _sprite.x=x;
     _sprite.y=y;
     _sprite.addChild(_textField);
     _sprite.addChild(_shape);
     _shape.graphics.beginFill(0xFFCC00);
     _shape.graphics.drawRect(0,0,width,height);
     _shape.graphics.endFill();
     _textField.text=text;
     if(type=="V"){
     VRoll();
     } else {
      HRoll();
     }
    }
    //横向滚动
    private function HRoll() : void{
    _textField.x=_shape.width;
    _textField.autoSize=TextFieldAutoSize.LEFT;
    _textField.y=0;
    _textField.multiline = false;
    _textField.wordWrap = false;
    _timer.addEventListener(TimerEvent.TIMER,Htimer_TIMER);
    _timer.start();
    }
    private function Htimer_TIMER(event :TimerEvent) : void{
    _textField.x-=1 ;
    if(_textField.x<=-_textField.width) {
       _textField.x=_shape.width;
    }
    }
    //垂直滚动
    private function VRoll() : void {
    _textField.x=0;
    _textField.y=_shape.height;
    _textField.multiline = true;
    _textField.wordWrap = true;
    _textField.width=_sprite.width;
    _timer.addEventListener(TimerEvent.TIMER,Vtimer_TIMER);
    _timer.start();
    }
    private function Vtimer_TIMER(event : TimerEvent) : void{
    _textField.y-=1;
    if(_textField.y<-_textField.height){
    _textField.y=_shape.height;
    }
    }
    //文本控制,可选
    public function setTextFormat(color : uint=0x000000,bold : Boolean = false,size : uint=12,
             font : String="Times New Roman") : void{
       _textFormat.color=color;
       _textFormat.bold=bold;
       _textFormat.size=size;
       _textFormat.font=font;       
    }
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值