影片剪辑位图化

[转自天地会的一篇文章]
一直困扰我的一个问题是如何降低影片剪辑在flash player中对资源的消耗,相信面对上百个带有寻路算法的人物需要被flash player渲染的情况,一定会让你抓狂,找到了这个位图化影片剪辑的类.部分解决了这个问题. 直接上代码.

package
{
    import flash.display.Bitmap;
    import flash.display.BitmapData;
    import flash.display.MovieClip;
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.geom.Matrix;
    import flash.geom.Rectangle;
    import flash.system.System;
    /**
     * 位图化的影片剪辑元件。
     * 
     */    
    public class BitmapMovieClip extends Sprite
    {
        private var _target:MovieClip;
        private var _movieClipBmp:Bitmap;
        private var _frameBmpDatas:Array;
        private var _currentFrame:int = 1;
        private var _totalFrames:int;
        
        public function BitmapMovieClip(target:MovieClip)
        {
            _target = target;
            _frameBmpDatas = new Array();
            _movieClipBmp = new Bitmap();
            _totalFrames = _target.totalFrames;
            addChild(_movieClipBmp);
            
            storeFrameAsBitmap();
            play();
        }
        /**
         * 播放动画。 
         * 
         */        
        public function play():void
        {
            addEventListener(Event.ENTER_FRAME, loopFrame, false, 0, true);
        }
        /**
         * 停止动画。 
         * 
         */
        public function stop():void{
            removeEventListener(Event.ENTER_FRAME, loopFrame);
        }
        /**
         * 跳转到目标帧并开始播放。 
         * @param frame
         * 
         */        
        public function gotoAndPlay(frame:int):void{
            _movieClipBmp.bitmapData = _currentFrame[frame];
            _currentFrame = frame;
            play();
        }
        /**
         * 跳转到目标帧并停止。 
         * @param frame
         * 
         */        
        public function gotoAndStop(frame:int):void{
            _movieClipBmp.bitmapData = _currentFrame[frame];
            _currentFrame = frame;
            stop();
        }
        /**
         * 实例的时间轴当前所处的帧号。  
         * @return 
         * 
         */        
        public function get currentFrame():int{
            return _currentFrame;
        }
        /**
         * 实例中帧的总数。 
         * 
         */        
        public function get totalFrame():int{
            return _totalFrames;
        }
        /**
         * 循环帧。 
         * @param evt
         * 
         */        
        private function loopFrame(evt:Event):void
        {
            _movieClipBmp.bitmapData = _frameBmpDatas[_currentFrame];
            _currentFrame = _currentFrame < _totalFrames - 1 ? _currentFrame + 1 : 0;
        }
        /**
         * 将目标影片剪辑缓存为位图数组。
         * 
         */        
        private function storeFrameAsBitmap():void
        {
            var r:Rectangle = _target.getBounds(_target);
            var i:int = 1;
            while(i <= _target.totalFrames)
            {
                _target.gotoAndStop(i)
                traversalChildrenFrame(_target, i);
                
                var bmpData:BitmapData = new BitmapData(r.width, r.height, true, 0);
                var m:Matrix = new Matrix();
                m.translate(-r.x, -r.y);
                m.scale(_target.scaleX, _target.scaleY);
                bmpData.draw(_target,m);
                _frameBmpDatas.push(bmpData);
                
                i++;
            }
            
            _movieClipBmp.x = r.x;
            _movieClipBmp.y = r.y;
        }
        /**
         * 遍历目标影片剪辑的所有子影片剪辑的帧。 
         * 
         */        
        private function traversalChildrenFrame(target:MovieClip, frame:int):void
        {
            var i:int = 0;
            var len:int = target.numChildren;
            while(i < len){
                var c:* = target.getChildAt(i);
                if(c is MovieClip){
                    traversalChildrenFrame(c, frame);
                    c.gotoAndStop(frame);
                }
                i++;
            }
        }
    }
}

 

posted @ 2012-03-01 12:26  黄龙  阅读(159)  评论(0)    收藏  举报