0

我有 cacheAsBitmap = true

和以下课程

package  utility{
    import flash.display.Bitmap;
    import flash.display.BitmapData;
    import flash.display.Sprite;
    import flash.geom.Matrix;
    import flash.geom.Rectangle;
    import flash.utils.getQualifiedClassName;


    public class CachedSprite extends Sprite {
        //Declare a static data cache
        protected static var cachedData:Object = { };

        public var clip:Bitmap;

        public function CachedSprite(asset:Class, centered:Boolean = false, scale:int = 2) {
            //Check the cache to see if we've already cached this asset
            var data:BitmapData = cachedData[getQualifiedClassName(asset)];

            if (!data) {
                // Not yet cached. Let's do it now

                // This should make "Class", "Sprite", and "Bitmap" data types all work.
                var instance:Sprite = new Sprite();
                instance.addChild(new asset());

                // Get the bounds of the object in case top-left isn't 0,0
                var bounds:Rectangle = instance.getBounds(this);

                // Optionally, use a matrix to up-scale the vector asset,
                // this way you can increase scale later and it still looks good.
                var m:Matrix = new Matrix();
                m.translate(-bounds.x, -bounds.y);
                m.scale(scale, scale);

                // This shoves the data to our cache. For mobiles in GPU-rendering mode,
                // also uploads automatically to the GPU as a texture at this point.
                data = new BitmapData(instance.width * scale, instance.height * scale, true, 0x0);
                data.draw(instance, m, null, null, null, true); // final true enables smoothing
                cachedData[getQualifiedClassName(asset)] = data;
            }

            // This uses the data already in the GPU texture bank, saving a draw/memory/push call:
            clip = new Bitmap(data, "auto", true);

            // Use the bitmap class to inversely scale, so the asset still
            // appear to be it's normal size
            clip.scaleX = clip.scaleY = 1 / scale;

            addChild(clip);

            if (centered) {
                // If we want the clip to be centered instead of top-left oriented:
                clip.x = clip.width / -2;
                clip.y = clip.height / -2;
            }

            // Optimize mouse children
            mouseChildren = false;
        }

        public function kill():void {
            // Just in case you want to clean up things the manual way
            removeChild(clip);
            clip = null;
        }
    }
}

有没有人可以向我解释不同之处?为什么我需要实现这个类而不是只使用 cacheAsBitmap = true?谢谢

4

2 回答 2

2

为避免重绘DisplayObjectif 已移动,您可以设置该cacheAsBitmap属性。如果设置为 true,Flash 运行时缓存显示对象的内部位图表示。

每当您将过滤器应用于显示对象时,cacheAsBitmap 属性都会自动设置为 true。最适合用于具有大部分静态内容且不经常缩放、旋转或更改 alpha 的显示对象,位图数据必须针对水平或垂直移动之外的所有操作重新计算。

自己缓存位图可以控制渲染生命周期。

在您的CachedSprite课程中,实际添加到显示列表的是 a Bitmap,而不是添加原始显示对象。与输入设备的任何交互都必须应用于缓存的精灵实例。

于 2012-10-26T03:49:12.487 回答
1

主要区别似乎在这条线上:

var data:BitmapData = cachedData[getQualifiedClassName(asset)];

此类保留对任何先前缓存的位图的静态引用。如果您有两个实例CachedSprite显示相同的位图数据(例如粒子),则此类将仅使用 的一个实例BitmapData,从而节省内存。

于 2012-10-26T03:35:55.780 回答