[As Functional code Tutorial 05] Typewriter effect

Source: Internet
Author: User

Train of thought: 1. Create a dynamic text box first with AS, for output text;

2. Use a timer to accumulate output the next character at intervals.

As code layer:

_root.createTextField("output", 1, 0, 0, 500, 400);
var txt_f:TextFormat = new TextFormat();
txt_f.font = "宋体";
//设置字体
txt_f.color = 0x333333;
//设置文字颜色
txt_f.size = 22;
//设置文字大小
txt_f.bold = true;
//是否加粗
output.setNewTextFormat(txt_f);
//将设置好的属性赋给output


var timer = setInterval(display, 500);
//计时器,每0.5秒调用一次display函数
var txt:String = "输入要显示的字符串";
var i = 0;
//初始化


function display() {
 output.text += txt.charAt(i);
 //每次文字累加
 if (i < txt.length-1){

i++;}

 else {
  clearInterval(timer);
 }
}
//每隔一段时间输出下一个字符,全部输出后,清除掉计时器

Flash charge 1: The string is the same as the array subscript, starting at 0.

Flash charge 2: String common methods:

(1) String.charat: Returns a character in a string

s=new String("ABCDE");

trace(s.charAt(3));

//输出结果为"D"

(2) String.charcodeat: Returns the codewords of a character in a string

s=new String("ABCDE");

trace(s.charCodeAt(3));

//输出结果为68 .

(3) String.Concat: Connecting to other strings

a=new String("CAT,");

b=new String("PIG,")

c=new String("DOG")

trace(a.concat(b,c))

//输出结果为"CAT,PIG,DOG" .

(4) String.IndexOf: Looking for substrings, and returning pointers

s=new String("ABCDEF");

trace(s.indexOf("CDE"))

//输出结果为2 .

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.