我有一个类使用两个与此类似的加载器加载两个swfs(注意新的ApplicationDomain):
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)))
加载的swfs实际上是工厂:
public class Child1Factory extends Sprite
{
public function Child1Factory() {}
public function getChild1():Child1 {
return new Child1()
}
}
一旦加载了两个swf,我就将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。)仅当我通过互联网或本地网络加载swfs时才会抛出错误。如果swfs在同一台计算机上,则不会抛出它。
2。)加载child1但不加载child2或visa virsa没有任何错误。
3.。)当发生错误时,根据我的调试器,MC_CircleGroup的子节点类型为MovieClip,而不是MC_Circle。
4。)每次运行应用程序时都不会抛出错误。它只是每隔几次。为了一致地得到错误,我必须通过在加载swfs后再次调用加载器来循环加载。
可根据要求提供完整的源代码。
谢谢! 添
更新
加载器文件中的完整代码如下所示:
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 好吧,这变得更加奇怪了。在@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 :(得分: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;
}