需要对flash中的打字机效果进行一些校正



我使用的是打字效果,因为我需要降低文字打印的速度。我不知道如何降低内容打印的速度。

这是代码

    var format : TextFormat = new TextFormat();
    format.size = 14;
    format.font = "Arial";
    format.bold = true;
    format.color = 0x00000;
    var _textField : TextField = new TextField();
    _textField.width = 400;
    _textField.height = 200;
    _textField.selectable = false;
    _textField.wordWrap = true;
    _textField.defaultTextFormat = format;
    _textField.x = _textField.y =10;
    addChild(_textField);

    var _textLoader:URLLoader = new URLLoader(new URLRequest("text.txt"));
    _textLoader.addEventListener(Event.COMPLETE, Init, false, 0, true);

    function Init(e:Event):void
    {
      var _text = e.target.data; 
      _letters = _text.split('');
      addEventListener(Event.ENTER_FRAME, Write, false, 0, true); 
    }
    function Write(e:Event):void
    {
      if (_counter < _letters.length)
      {
             _textField.appendText(_letters[_counter]); 
             _counter++; 
      }
}

好吧,因为您使用的是EnterFrame事件,所以您可以降低SWF的FPS,但这可能不是您想要的,因为它会降低SWF中所有内容的速度,而不仅仅是文本字段打印。

或者,你可以只保留一些计数器变量来跟踪帧,并且每10帧左右只追加一次文本。例如:

var currFrame:int = 0;
//...
function Write(e:Event):void
{
    currFrame++;//increase the counter
    if(currFrame > 10)
    {//only write a letter every 10 frames
        currFrame = 0;//reset the counter
        if (_counter < _letters.length)
        {
            _textField.appendText(_letters[_counter]); 
            _counter++; 
        }
    }
}

调整"10"以更改速度(较大的数字=较慢)

首先,我没有在代码中的任何地方看到变量_counter的声明。也就是说,它们有几种方法可以让它变得更慢,这是一种可能性。

如果"_counter"var小于字符串=>的长度,则添加一个字母。

因此,要使速度变慢,你可以说:

如果"_counter"var小于字符串=>的长度,请等待2秒,然后添加一个字母。

例如,您可以使用setInterval函数来执行此操作:

www.ilike2flash.com/2009/07/time-delay-in-actionscript-3.html

因此,您的代码将类似于以下内容:

    var _prevCounter = "";
        function Write(e:Event):void
    {
        if (_counter < _letters.length && _counter != _prevCounter)
        {
            setInterval(addLetter, 3000);
            _counter++;
        }
    }
    function addLetter()
    {
        _textField.appendText(_letters[_counter]);
    }

这没有经过测试,可能会导致一些错误,如果您不了解要复制/粘贴的内容,请不要复制/粘贴

祝你好运&希望这对你有帮助!

只需使用Timer事件而不是ENTER_FRAME事件。您可以将"文字打印速度"定义为计时器延迟。

const DELAY_BETWEEN_LETTERS:int = 100; // here you are setting 100 ms between each letter output
var timer:Timer = new Timer(DELAY_BETWEEN_LETTERS);
function Init(e:Event):void
{
  var _text = e.target.data; 
  _letters = _text.split('');
  timer.addEventListener(TimerEvent.TIMER, Write);
  timer.start();
}
function Write(e:TimerEvent):void
{
  if (_counter < _letters.length)
  {
         _textField.appendText(_letters[_counter]); 
         _counter++; 
  }
}

您也可以使用计时器,如以下示例所示:

package 
{
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.events.TextEvent;
    import flash.text.TextField;
    import flash.text.TextFieldAutoSize;
    public class Main extends Sprite 
    {
        public function Main():void 
        {
            if (stage) init();
            else addEventListener(Event.ADDED_TO_STAGE, init);
        }// end function
        private function init(e:Event = null):void 
        {
            removeEventListener(Event.ADDED_TO_STAGE, init);
            var textField:TypewriterTextField = new TypewriterTextField();
            textField.autoSize = TextFieldAutoSize.LEFT;
            textField.border = true;
            addChild(textField);
        }// end function
    }// end class
}// end package
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.events.MouseEvent;
import flash.events.TimerEvent;
import flash.text.TextField;
import flash.utils.Timer;
class TypewriterTextField extends TextField {
    public function TypewriterTextField() {
        this.addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
    }// end function
    private function onAddedToStage(e:Event):void {
        stage.addEventListener(KeyboardEvent.KEY_UP, onKeyUp);
    }// end function
    private function onKeyUp(e:KeyboardEvent):void {
        e.preventDefault();
        if (stage.focus == this) {
            var char:String = String.fromCharCode(e.charCode);
            if (char.match(/[a-zA-Z0-9sn]/) ||  e.charCode == 8) {
                var timer:CustomTimer = new CustomTimer(e.charCode, 500, 1);
                timer.addEventListener(TimerEvent.TIMER, onTimer);
                timer.start();
            }// end if
        }// end if
    }// end function
    private function onTimer(e:TimerEvent):void {
        var customTimer:CustomTimer = e.target as CustomTimer;
        customTimer.removeEventListener(TimerEvent.TIMER, onTimer);
        var charCode:uint = customTimer.object as uint;
        var char:String = String.fromCharCode(charCode);
        if (char.match(/[a-zA-Z0-9sn]/)) {
            this.text += char;
        }
        else {
            this.text = this.text.slice(0, -1);
        }// end else if
    }// end function
}// ende class
class CustomTimer extends Timer {
    private var _cbject:Object;
    public function get object():Object {
        return this._cbject;
    }// end function
    public function CustomTimer(object:Object, delay:Number, repeatCount:int = 0) {
        super(delay, repeatCount);
        _cbject = object;
    }// end function
}// end class

最新更新