2011-07-20 35 views
0

我試圖重複使用greensocks LoaderMax加載的SWF,但我似乎並沒有能夠重複的裝載LoaderMax

我使用下面的代碼的SWF:

private var _assetCache : Dictionary; 

public function getAssetById(assetId:String):DisplayObject 
{ 
    var asset:DisplayObject; 

    if (!_assetCache[assetId]) 
    { 
     _assetCache[assetId] = LoaderMax.getContent(assetId).rawContent; 
    } 

    asset = _assetCache[assetId]; 

    // duplicate bitmap 
    if (asset is Bitmap) 
    { 
     var bmd:BitmapData = new BitmapData(asset.width, asset.height, true, 0); 
     return new Bitmap(bmd, "auto", true); 
    } 
    // otherwise return 
    return SpriteUtils.duplicateDisplayObject(asset); 
    //return asset; // if previous line is commented out, this works well but there will be only a single copy of the loaded swf, kinda negating the use of a cache 
} 

這是SpriteUtils.duplicateDisplayObject(資產)(從this

static public function duplicateDisplayObject(target:DisplayObject, autoAdd:Boolean = false):DisplayObject 
{ 
    // create duplicate 
    var targetClass:Class = Object(target).constructor; 
    var duplicate:DisplayObject = new targetClass(); 
    trace(duplicate, duplicate.height); // traces [MovieClip, 0] 
    // duplicate properties 
    duplicate.transform = target.transform; 
    duplicate.filters = target.filters; 
    duplicate.cacheAsBitmap = target.cacheAsBitmap; 
    duplicate.opaqueBackground = target.opaqueBackground; 
    if (target.scale9Grid) 
    { 
     var rect:Rectangle = target.scale9Grid; 
     // WAS Flash 9 bug where returned scale9Grid is 20x larger than assigned 
     // rect.x /= 20, rect.y /= 20, rect.width /= 20, rect.height /= 20; 
     duplicate.scale9Grid = rect; 
    } 

    // add to target parent's display list 
    // if autoAdd was provided as true 
    if (autoAdd && target.parent) 
    { 
     target.parent.addChild(duplicate); 
    } 
    return duplicate; 
} 

採取了我根本沒有複製其返回從_assetCache(這是一本字典)的資產,它的工作原理和跟蹤s作爲MovieClip,但是當我嘗試複製它時,即使跡線告訴我重複是一個movieclip。注意要加載的剪輯是在時間軸上的根的階段簡單的矢量圖形

在此先感謝

奧比

回答

1

我不認爲簡單地調用構造函數工作。

嘗試這樣做(我假定某些以前的知識,因爲你能夠得到rawContent以上):

1)在二進制模式下使用的DataLoader加載數據....在LoaderMax的背景下它將如下所示:swfLoader = new LoaderMax({name:「swfLoader」,onProgress:swfProgressHandler,onChildComplete:swfLoaded,auditSize:false,maxConnections:2}); swfLoader.append(new DataLoader(url,{format:'binary'})); (主要是使用格式='二進制'的DataLoader)

2)完成後,將ByteArray存儲到您的字典中。上述代碼的第一部分基本上沒有變化,儘管字節陣列可能是內容而不是原始內容

3)每當需要重複副本時,請使用ByteArray上的Loader.loadBytes()。即var ldr:Loader = new Loader(); ldr.contentLoaderInfo.addEventListener(Event.COMPLETE,swfReallyLoaded); ldr.loadBytes(_assetCache [由assetid]);與所有加載一樣,可能需要設置一個LoaderContext,並且如果在AIR-allowLoadBytesCodeExecution = true; allowCodeImport = true;

+0

謝謝 - 我會測試一下 – obie