13 programming skills commonly used in Flash as3

Source: Internet
Author: User

[Transferred from E liangshi Yiyou network]

The following are 13 tips commonly used in Flash as3. You can learn the flash as3 programming course example tutorial and learn more about the related knowledge. You can log on to e liangshi Yiyou.

1: Convert string to boolean

VaR S: String = "true ";

VaR B: Boolean = (S = "true ");

2: Clear all sub-objects

While (container. numchildren> 0)

{3 Container. removechildat (0 );

}

3: for objects that do not require mouse Interaction

Set the attributes mousechildren and mouseenabled.

4: Use the Vector class instead of the array class as much as possible. The read/write access speed of the Vector class is faster than that of the array class.

5: You can further optimize it by assigning a specific length to the vector and setting its length to a fixed value.

// Specify a fixed length and initialize its length

VaR coordinates: vector. = new vector. (300000, true );

VaR started: Number = gettimer ();

For (var I: Int = 0; I <300000; I ++)

{

Coordinates [I] = math. Random () * 1024;

}

Trace (gettimer ()-started );

// Output: 48

6. Store reused values in constants to further optimize the above instances.

// Store the reused value to maintain code easily

Const max_num: Int = 300000;

VaR coordinates: vector. = new vector. (max_num, true );

VaR started: Number = gettimer ();

For (var I: Int = 0; I <max_num; I ++)

{

Coordinates [I] = math. Random () * 1024;

}

Trace (gettimer ()-started );

// Output: 47
7: Use the lock () and unlock () Methods of bitmapdata to speed up the operation.

8: For textfield objects, use the appendtext () method instead of the + = Operator.

9: using the brackets operator may reduce performance. You can avoid using this operator to store your references in local variables. The following code example demonstrates the low efficiency of using the brackets OPERATOR:

VaR LNG: Int = 5000;

VaR arraysprite: vector. = new vector. (LNG, true );

VaR I: int;

For (I = 0; I <LNG; I ++)

{

Arraysprite [I] = new sprite ();

}

VaR started: Number = gettimer ();

For (I = 0; I <LNG; I ++)

{

Arraysprite [I]. x = math. Random () * stage. stagewidth;

Arraysprite [I]. Y = math. Random () * stage. stageheight;

Arraysprite [I]. Alpha = math. Random ();

Arraysprite [I]. Rotation = math. Random () * 360;

}

Trace (gettimer ()-started );

// Output: 16

The following optimized versions reduce the use of the brackets OPERATOR:

VaR LNG: Int = 5000;

VaR arraysprite: vector. = new vector. (LNG, true );

VaR I: int;

For (I = 0; I <LNG; I ++)

{

Arraysprite [I] = new sprite ();

}

VaR started: Number = gettimer ();

VaR currentsprite: SPRITE;

For (I = 0; I <LNG; I ++)

{

Currentsprite = arraysprite [I];

Currentsprite. x = math. Random () * stage. stagewidth;

Currentsprite. Y = math. Random () * stage. stageheight;

Currentsprite. Alpha = math. Random ();

Currentsprite. Rotation = math. Random () * 360;

}

Trace (gettimer ()-started );

// Output: 9
10: Try to use Inline code to reduce the number of function calls in the code. For example:

1 currentvalue> 0? Currentvalue:-currentvalue;

Faster than the following
Math. Abs (currentvalue );

11: avoid statements in the calculation cycle.

Statements in the calculation cycle can also be optimized. The following code traverses the array but is not optimized, because the length of the array needs to be calculated during each time:

For (var I: Int = 0; I <myarray. length; I ++)

{

}

It is best to store this value and reuse it:

VaR LNG: Int = myarray. length;

For (var I: Int = 0; I <LNG; I ++)

{

}

12: Use the reverse order for the while loop. The speed of the while loop in reverse order is faster than that of the forward loop:

VaR I: Int = myarray. length;

While (-- I>-1)

{

}
13: Generally, the performance can be improved with the lowest possible frame rate.


This article is from the blog, please be sure to keep this source http://yuguotianqing.blog.51cto.com/9292883/1549439

13 programming skills commonly used in Flash as3

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.