转自猫粮博客:http://www.xflex.cn/blog/archives/612.html
程序代码来之Advanced ActionScript Animation。 下面这个是没有优化过的
package
{
import flash.display.Sprite;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.events.Event;
import flash.text.TextField;
import flash.text.TextFormat;
[SWF(width=800, height=800, backgroundColor=0xffffff)]
public class Container3D extends Sprite
{
private var _sprite:Sprite;
private var _n:Number = 0;
public function Container3D()
{
stage.align = StageAlign.TOP_LEFT;
stage.scaleMode = StageScaleMode.NO_SCALE;
_sprite = new Sprite();
_sprite.y = stage.stageHeight / 2;
for(var i:int = 0; i < 100; i++)
{
var tf:TextField = new TextField();
tf.defaultTextFormat = new TextFormat("Arial", 40);
tf.text = String.fromCharCode(65 + Math.floor(Math.random() * 25));
tf.selectable = false;
tf.x = Math.random() * 300 - 150;
tf.y = Math.random() * 300 - 150;
tf.z = Math.random() * 1000;
_sprite.addChild(tf);
}
addChild(_sprite);
addEventListener(Event.ENTER_FRAME, onEnterFrame);
}
private function onEnterFrame(event:Event):void
{
_sprite.x = stage.stageWidth / 2 + Math.cos(_n) * 200;
_n += .05;
}
}
}
加了行 cacheBitmap = true 后,少了20%这样
package
{
import flash.display.Sprite;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.events.Event;
import flash.text.TextField;
import flash.text.TextFormat;
[SWF(width=800, height=800, backgroundColor=0xffffff)]
public class Container3D extends Sprite
{
private var _sprite:Sprite;
private var _n:Number = 0;
public function Container3D()
{
stage.align = StageAlign.TOP_LEFT;
stage.scaleMode = StageScaleMode.NO_SCALE;
_sprite = new Sprite();
_sprite.y = stage.stageHeight / 2;
_sprite.cacheAsBitmap = true;
for(var i:int = 0; i < 100; i++)
{
var tf:TextField = new TextField();
tf.defaultTextFormat = new TextFormat("Arial", 40);
tf.text = String.fromCharCode(65 + Math.floor(Math.random() * 25));
tf.selectable = false;
tf.x = Math.random() * 300 - 150;
tf.y = Math.random() * 300 - 150;
tf.z = Math.random() * 1000;
_sprite.addChild(tf);
}
addChild(_sprite);
addEventListener(Event.ENTER_FRAME, onEnterFrame);
}
private function onEnterFrame(event:Event):void
{
_sprite.x = stage.stageWidth / 2 + Math.cos(_n) * 200;
_n += .05;
}
}
}
把所有的字母都draw成bitmap以后再加去显示对象里面,cpu 占用率奇迹般地只有10%了..
而且我发现,关掉鼠标事件很有必要...
package
{
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.Sprite;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.events.Event;
import flash.text.TextField;
import flash.text.TextFormat;
[SWF(width=800, height=800, backgroundColor=0xffffff)]
public class Container3D extends Sprite
{
private var _sprite:Sprite;
private var _n:Number = 0;
public function Container3D()
{
stage.align = StageAlign.TOP_LEFT;
stage.scaleMode = StageScaleMode.NO_SCALE;
_sprite = new Sprite();
_sprite.y = stage.stageHeight / 2;
_sprite.cacheAsBitmap = true;
_sprite.mouseChildren = false;
_sprite.mouseEnabled = false;
for(var i:int = 0; i < 100; i++)
{
var tf:TextField = new TextField();
tf.defaultTextFormat = new TextFormat("Arial", 40);
tf.text = String.fromCharCode(65 + Math.floor(Math.random() * 25));
tf.selectable = false;
tf.width = tf.textWidth;
tf.height = tf.textHeight;
var bdd:BitmapData = new BitmapData(tf.width, tf.height, true, 0xFFFFFF);
bdd.draw(tf);
var bd:Bitmap = new Bitmap(bdd);
bd.x = Math.random() * 300 - 150;
bd.y = Math.random() * 300 - 150;
bd.z = Math.random() * 1000;
_sprite.addChild(bd);
}
addChild(_sprite);
addEventListener(Event.ENTER_FRAME, onEnterFrame);
}
private function onEnterFrame(event:Event):void
{
_sprite.x = stage.stageWidth / 2 + Math.cos(_n) * 200;
_n += .05;
}
}
}