2013-01-22 18 views
2

我有它加載使用類似的雙裝載機兩個SWF這一個(注意新ApplicationDomain中)類:使用flash.display.Loader intermitently引起加載的SWF的孩子鬆散型

var child1Loader:Loader = new Loader() 
child1Loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onChild1Complete) 
child1Loader.load(new URLRequest("http://mywebsite.com/assets/load_test/Child1Factory.swf?" + Math.random()), new LoaderContext(false, new ApplicationDomain(ApplicationDomain.currentDomain))) 

加載的主權財富基金是在其實工廠:

public class Child1Factory extends Sprite 
{ 

    public function Child1Factory() {} 

    public function getChild1():Child1 { 
     return new Child1() 
    } 

} 

一旦兩個主權財富基金被載入我添加child1和的child2的階段,像這樣:

var child1:MovieClip = child1Factory.getChild1() 
var child2:MovieClip = child2Factory.getChild2() 
addChild(child1) 
addChild(child2) 

Child1和child2看起來非常相似但不完全相同。 Child1看起來像這樣(注意ADDED_TO_STAGE監聽器):

public class Child1 extends MovieClip 
{ 

    public function Child1() 
    { 
     Security.allowDomain("*") 
     if (stage) onAddedToStage(null) 
     else addEventListener(Event.ADDED_TO_STAGE, onAddedToStage)   
    } 

    private function onAddedToStage(e:Event = null):void { 
     removeEventListener(Event.ADDED_TO_STAGE, onAddedToStage) 
     addChild(new MC_CircleGroup()) 
     runChild1Test() 
    } 

    private function runChild1Test():void { 
     var circles:Array = findChildrenOfType(MC_Circle, this) 
     if (circles.length == 0) { 
      throw new Error("Oh no!") 
     } 
    } 

    private function findChildrenOfType(type:Class, container:DisplayObjectContainer):Array { 
     var toReturn:Array = [] 
     for (var childIndex:int = 0; childIndex < container.numChildren; childIndex++) { 
      var child:DisplayObject = container.getChildAt(childIndex) 
      if (child is type) { 
       toReturn.push(child) 
      } else if (child is DisplayObjectContainer) { 
       toReturn = toReturn.concat(findChildrenOfType(type, child as DisplayObjectContainer)) 
      } 
     } 
     return toReturn 
    } 

} 

和child2類似,只是讀取正方形的圓圈。這兩個類都有不同的swcs,其中包含相關的MC_SquareGroup或MC_CircleGroup影片剪輯。這些孩子有MC_Square或MC_Circle類型的四個孩子。 findChildrenOfType函數通常返回這四個實例的數組。

錯誤「On no!」每運行5次應用程序就會拋出大約1次。現在有人爲什麼?或者現在有更好的解決方法嗎?

一些注意事項:

1)當我加載SWF文件在互聯網上或在本地網絡上的誤差僅爲拋出。如果swfs位於同一臺計算機上,則不會拋出它。

2.)加載child1但不是child2或簽證virsa的工作沒有任何錯誤。

3.)當發生錯誤時,根據我的調試器,MC_CircleGroup的子類型爲MovieClip,而不是MC_Circle。

4.)每次運行應用程序時都不會引發錯誤。它只有每隔幾次。爲了一致地得到錯誤,我必須在兩個swf加載後再次調用加載器來循環加載。

可根據要求提供完整的源代碼。

謝謝! 添

UPDATE

在加載文件的完整代碼如下所示:

public class Main extends Sprite 
{ 
    private var child1Factory:Object; 
    private var child1Loaded:Boolean = false; 
    private var child2Factory:Object; 
    private var child2Loaded:Boolean = false; 

    public function Main():void 
    { 
     Security.allowDomain("*") 
     if (stage) loadChildren(); 
     else addEventListener(Event.ADDED_TO_STAGE, loadChildren); 
    } 

    private function loadChildren(e:Event = null):void 
    { 
     removeEventListener(Event.ADDED_TO_STAGE, loadChildren); 

     child1Loaded = false 
     child2Loaded = false 

     var child1Loader:Loader = new Loader() 
     child1Loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onChild1Complete) 
     child1Loader.load(new URLRequest("http://mywebsite.com/assets/load_test/Child1Factory.swf?" + Math.random()), new LoaderContext(false, new ApplicationDomain(ApplicationDomain.currentDomain))) 

     var child2Loader:Loader = new Loader() 
     child2Loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onChild2Complete) 
     child2Loader.load(new URLRequest("http://mywebsite.com/assets/load_test/Child2Factory.swf?" + Math.random()), new LoaderContext(false, new ApplicationDomain(ApplicationDomain.currentDomain))) 


    } 

    private function onChild1Complete(e:Event):void { 
     child1Factory = e.currentTarget.content 
     child1Loaded = true 
     if (child2Loaded) { 
      onLoadComplete() 
     } 

    } 

    private function onChild2Complete(e:Event):void { 
     child2Factory = e.currentTarget.content 
     child2Loaded = true 
     if (child1Loaded) { 
      onLoadComplete() 
     } 

    } 

    private function onLoadComplete():void { 
     var child1:MovieClip = child1Factory.getChild1() 
     var child2:MovieClip = child2Factory.getChild2() 
     addChild(child1) 
     addChild(child2) 
     loadChildren(null) 
    } 

} 

更新2 OK,這事變得更加怪異。在@DavidMear之後,我更新了它,這樣孩子們就被添加到了onChildXComplete函數中,並且突然間它減少了很多次。它還是破很偶然,但:

public class Main extends Sprite 
{ 
    private var child1Factory:Object; 
    private var child1Loaded:Boolean = false; 
    private var child2Factory:Object; 
    private var child2Loaded:Boolean = false; 

    public function Main():void 
    { 
     Security.allowDomain("*") 
     if (stage) loadChildren(); 
     else addEventListener(Event.ADDED_TO_STAGE, loadChildren); 
    } 

    private function loadChildren(e:Event = null):void 
    { 
     removeEventListener(Event.ADDED_TO_STAGE, loadChildren); 

     child1Loaded = false 
     child2Loaded = false 

     var child1Loader:Loader = new Loader() 
     child1Loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onChild1Complete) 
     child1Loader.load(new URLRequest("http://mywebsite.com/assets/load_test/Child1Factory.swf?" + Math.random()), new LoaderContext(false, new ApplicationDomain(ApplicationDomain.currentDomain))) 

     var child2Loader:Loader = new Loader() 
     child2Loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onChild2Complete) 
     child2Loader.load(new URLRequest("http://mywebsite.com/assets/load_test/Child2Factory.swf?" + Math.random()), new LoaderContext(false, new ApplicationDomain(ApplicationDomain.currentDomain))) 


    } 

    private function onChild1Complete(e:Event):void { 
     child1Factory = e.currentTarget.content 
     child1Loaded = true 
     var child1:MovieClip = child1Factory.getChild1() 
     addChild(child1) 
     if (child2Loaded) { 
      onLoadComplete() 
     } 

    } 

    private function onChild2Complete(e:Event):void { 
     child2Factory = e.currentTarget.content 
     child2Loaded = true 
     var child2:MovieClip = child2Factory.getChild2() 
     addChild(child2) 
     if (child1Loaded) { 
      onLoadComplete() 
     } 

    } 

    private function onLoadComplete():void { 
     loadChildren(null) 
    } 

} 

更新3

此代碼引發錯誤在許多相同的頻率原始和對定時器處理程序的一個堆棧跟蹤點。如果堆棧跟蹤沒有指向onLoadComplete的計時器,它也會拋出錯誤...即,測試函數在拋出錯誤之前已經在onChildXComplete函數中成功運行一次。現在我是真的是困惑。

public class Main extends Sprite 
{ 
    private var child1Factory:Object; 
    private var child1Loaded:Boolean = false; 
    private var child2Factory:Object; 
    private var child2Loaded:Boolean = false; 

    public function Main():void 
    { 
     Security.allowDomain("*") 
     if (stage) loadChildren(); 
     else addEventListener(Event.ADDED_TO_STAGE, loadChildren); 
    } 

    private function loadChildren(e:Event = null):void 
    { 
     removeEventListener(Event.ADDED_TO_STAGE, loadChildren); 

     child1Loaded = false 
     child2Loaded = false 

     var child1Loader:Loader = new Loader() 
     child1Loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onChild1Complete) 
     child1Loader.load(new URLRequest("http://mywebsite.com/assets/load_test/Child1Factory.swf?" + Math.random()), new LoaderContext(false, new ApplicationDomain(ApplicationDomain.currentDomain))) 

     var child2Loader:Loader = new Loader() 
     child2Loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onChild2Complete) 
     child2Loader.load(new URLRequest("http://mywebsite.com/assets/load_test/Child2Factory.swf?" + Math.random()), new LoaderContext(false, new ApplicationDomain(ApplicationDomain.currentDomain))) 


    } 

    private function onChild1Complete(e:Event):void { 
     child1Factory = e.currentTarget.content 
     child1Loaded = true 
     var child1:MovieClip = child1Factory.getChild1() 
     addChild(child1) 
     if (child2Loaded) { 
      onLoadComplete() 
     } 

    } 

    private function onChild2Complete(e:Event):void { 
     child2Factory = e.currentTarget.content 
     child2Loaded = true 
     var child2:MovieClip = child2Factory.getChild2() 
     addChild(child2) 
     if (child1Loaded) { 
      onLoadComplete() 
     } 

    } 

    private function onLoadComplete():void { 

     var timer1:Timer = new Timer(500, 1) 
     timer1.addEventListener(TimerEvent.TIMER_COMPLETE, function(e:TimerEvent):void { 
      var child1:MovieClip = child1Factory.getChild1() 
      addChild(child1) 
     }) 
     timer1.start() 

     var timer2:Timer = new Timer(1000, 1) 
     timer2.addEventListener(TimerEvent.TIMER_COMPLETE, function(e:TimerEvent):void { 
      var child2:MovieClip = child2Factory.getChild2() 
      addChild(child2) 
      loadChildren(null) 
     }) 
     timer2.start()   

    } 

} 
+0

事實上,它間歇性地發生,只有當通過網絡加載時,我懷疑這是與測試運行時間有關。在swf完全加載之前,可能會將'Child1'添加到階段,因此在'onChild1Complete'上運行測試會有所作爲嗎? –

+0

感謝您的回覆@DavidMear。我已經用loader類的完整代碼更新了這個問題。在調用onChild1Complete之前,不應將Child1添加到階段。我已經嘗試過分開測試,但不幸的是沒有喜悅。雖然我很同意你的評價。它看起來好像沒有完全加載。是否有另一個我可以聽的事件? –

+0

Hi @DavidMear。你能看看我上面的第二個更新。它變得越來越奇怪。你會期望它有不同的表現嗎? –

回答

0

我想,也許findChildrenOfType方法是不完全正確遞歸調用,因爲你是在重置每個呼叫的toReturn陣列。也許這樣的事情可能會更好?

function findChildrenOfType(type:Class, container:DisplayObjectContainer, toReturn:Array = null):Array { 
    if(!toReturn) toReturn = []; 
    for (var childIndex:int = 0; childIndex < container.numChildren; childIndex++) { 
     var child:DisplayObject = container.getChildAt(childIndex) 
     if (child is type) { 
      toReturn.push(child); 
     } else if (child is DisplayObjectContainer) { 
      findChildrenOfType(type, child as DisplayObjectContainer, toReturn); 
     } 
    } 
    return toReturn; 
} 
+0

感謝您花時間看這個@Dezza。我很確定原始功能正常工作。就像我說的那樣,它只是偶爾打破。你的功能也可以從它的外觀起作用,並且都做同樣的事情。在我的函數中,數組不需要保存。每次調用函數時都是不同的數組。結果數組連接在一起。 –

+0

Child類完全獨立工作,或者只有其中一個被加載,或者它們從同一臺計算機上加載。即使兩臺計算機都是從另一臺計算機加載的,他們也會花費很多時間。這是一個反常的錯誤,所以我很確定邏輯本身沒有任何問題。 –

相關問題