asAs在cacheAsBitmap与CachedSprite之间有所不同

时间:2012-10-26 02:01:15

标签: actionscript-3 bitmap

我有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?感谢

2 个答案:

答案 0 :(得分:2)

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

每当将过滤器应用于显示对象时,cacheAsBitmap属性都会自动设置为true。最好与主要使用静态内容并且不经常缩放,旋转或更改alpha的显示对象一起使用,必须重新计算位图数据以用于除水平或垂直移动之外的所有操作。

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

CachedSprite课程中,实际添加到显示列表的内容是Bitmap,而不是添加原始显示对象。任何带有输入设备的interaction都必须应用于缓存的精灵实例。

答案 1 :(得分:1)

主要区别在于这一行:

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

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