我正在使用打字效果,因为我需要降低文字打印的速度。我不知道如何降低内容打印的速度。
这是代码
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++;
}
}
答案 0 :(得分:0)
好吧,既然你正在使用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”以改变速度(更大的数字=更慢)
答案 1 :(得分:0)
首先,我没有看到变量_counter在代码中的任何地方声明。 也就是说,它们是使它变慢的几种方法,这是其中一种可能性。
如果“_counter”var小于字符串的长度=&gt;添加一封信。
所以为了让这个变慢,你可以说:
如果“_counter”var小于字符串的长度=&gt;等待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]);
}
这未经过测试,可能会导致一些错误,如果您不明白要复制/粘贴的内容,请不要复制/粘贴!
祝你好运&amp;希望这能帮到你的路!答案 2 :(得分:0)
只需使用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++;
}
}
答案 3 :(得分:0)
您还可以使用以下示例中的计时器:
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-9\s\n]/) || 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-9\s\n]/)) {
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