蛋疼的性能优化

转自猫粮博客: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;
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值