时间:2012-06-27 18:48:55 来源:电脑十万个为什么 作者:网络 浏览:
Flash as3.0 教程:必看的13个实用小技巧
1:String转换成Boolean
1vars:String="true";
2varb:Boolean=(s=="true");
2:清除所有子对象
1while(container.numChildren>0)
2{
3container.removeChildAt(0);
4}
3:对于不需要鼠标交互的对象设置属性mouseChildren,mouseEnabled。
4:尽可能使用Vector类而不是Array类,Vector类的读写访问速度比Array类快。
5:通过为矢量分配特定长度并将其长度设为固定值,可进一步优化。 1//Specifyafixedlengthandinitializeitslength2varcoordinates:Vector.<Number>=newVector.<Number>(300000,true);
3varstarted:Number=getTimer();
4for(vari:int=0;i<300000;i++)
5{
6coordinates[i]=Math.random()*1024;
7}
8trace(getTimer()-started);
9//output:48
6:将重用的值存储在常量,可对上面实例进一步优化。
1//Storethereusedvaluetomaintaincodeeasily2constMAX_NUM:int=300000;
3varcoordinates:Vector.<Number>=newVector.<Number>(MAX_NUM,true);
4varstarted:Number=getTimer();
5for(vari:int=0;i<MAX_NUM;i++)
6{
7coordinates[i]=Math.random()*1024;
8}
9trace(getTimer()-started);
10//output:47
7:使用BitmapData的lock()和unlock()技巧加快运行速度。
8:对于TextField对象,请使用appendText()技巧,而不要使用+=运算符。
9:使用中括号运算符可能会降低性能。将您的引用存储在本地变量中可避免使用该运算符。以下代码示例演示了使用中括号运算
符的效率很低:
1varlng:int=5000;
2vararraySprite:Vector.<Sprite>=newVector.<Sprite>(lng,true);
3vari:int;
4for(i=0;i<lng;i++)
5{
6arraySprite[i]=newSprite();
7}
8varstarted:Number=getTimer();
9for(i=0;i<lng;i++)
10{
11arraySprite[i].x=Math.random()*stage.stageWidth;
12arraySprite[i].y=Math.random()*stage.stageHeight;
13arraySprite[i].alpha=Math.random();
14arraySprite[i].rotation=Math.random()*360;
15}
16trace(getTimer()-started);
17//output:16
以下优化的版本减少了对中括号运算符的使用:
1varlng:int=5000;
2vararraySprite:Vector.<Sprite>=newVector.<Sprite>(lng,true);
3vari:int;
4for(i=0;i<lng;i++)
5{
6arraySprite[i]=newSprite();
7}
8varstarted:Number=getTimer();
9varcurrentSprite:Sprite;
10for(i=0;i<lng;i++)
11{
12currentSprite=arraySprite[i];
13currentSprite.x=Math.random()*stage.stageWidth;
14currentSprite.y=Math.random()*stage.stageHeight;
15currentSprite.alpha=Math.random();
16currentSprite.rotation=Math.random()*360;
17}
18trace(getTimer()-started);
19//output:9
10:尽可能使用内联代码以减少代码中函数的调用次数。例如:
1currentValue>0?currentValue:-currentValue;
比下面这种快
1Math.abs(currentValue);
11:避免计算循环中的语句。
不计算循环中的语句也可实现优化。以下代码遍历数组,但未进行优化,因为在每次遍历时都需要计算数组长度:
1for(vari:int=0;i<myArray.length;i++)
2{
3}
最好存储该值并重复使用:
1varlng:int=myArray.length;
2for(vari:int=0;i<lng;i++)
3{
4}
12:对while循环使用相反的顺序。
以相反顺序进行while循环的速度比正向循环快:
1vari:int=myArray.length;
2while(--i>-1)
3{
4}
13:通常,使用尽可能低的帧速率可以提高性能。
相关文章推荐阅读:
网页flash不显示怎么办?
怎么解决adobe flash player 未成功安装?
无法安装flash player的activex解决技巧