2012-10-02 38 views
0

查看周圍SO的答案,但真的很掙扎。我已經嘗試了下面的代碼中的變體,即從舞臺切換到this.stage甚至root.stage,但沒有運氣。ArgumentError:錯誤#2025:提供的DisplayObject必須是調用者的孩子

任何幫助,非常感謝。

完整的錯誤我得到的是:

ArgumentError: Error #2025: The supplied DisplayObject must be a child of the caller. 
at flash.display::DisplayObjectContainer/setChildIndex() 
at flash.display::Stage/setChildIndex() 
at Banner/next_image_loaded() 

package { 
    import flash.display.*; 
    import flash.events.*; 
    import flash.net.*; 
    import flash.utils.*; 
    import gs.*; 
    import gs.easing.*; 

public class Banner extends MovieClip 
{ 
    var xml_url:URLRequest; 
    var xml_loader:URLLoader; 
    var img_loader:Loader; 
    var img_data:Array; 
    var img:Bitmap; 
    var new_img:Bitmap; 
    var strapline:strap; 
    var banner:bottom_banner; 
    var button:tech_button; 
    var current_image:Number = -1; 
    var done:Boolean = false; 

    public function Banner() 
    { 
     this.xml_url = new URLRequest("Banner.xml"); 
     this.xml_loader = new URLLoader(this.xml_url); 
     this.xml_loader.addEventListener(Event.COMPLETE, this.xml_loaded); 
     this.xml_loader.addEventListener(IOErrorEvent.IO_ERROR, this.err); 
     return; 
    }// end function 

    private function xml_loaded(event:Event) 
    { 
     this.img_data = new Array(); 
     var _loc_2:* = new XML(event.target.data); 
     var _loc_3:* = 0; 
     while (_loc_3 < _loc_2.image.length()) 
     { 

      this.img_data.push(_loc_2.image[_loc_3]); 
      _loc_3 = _loc_3 + 1; 
     } 
     this.img_data = this.randomise_array(this.img_data); 
     this.next_content(); 
     return; 
    }// end function 

    private function randomise_array(param1:Array) : Array 
    { 
     var _loc_2:* = new Array(); 
     while (param1.length > 0) 
     { 

      _loc_2.push(param1.splice(Math.floor(Math.random() * param1.length), 1)); 
     } 
     return _loc_2; 
    }// end function 

    private function next_content() 
    { 
     if (this.current_image == (this.img_data.length - 1)) 
     { 
      this.current_image = 0; 
     } 
     else 
     { 
      var _loc_2:* = this.current_image + 1; 
      this.current_image = _loc_2; 
     } 
     this.img_loader = new Loader(); 
     this.img_loader.contentLoaderInfo.addEventListener(Event.COMPLETE, this.next_image_loaded); 
     this.img_loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, this.err); 
     this.img_loader.load(new URLRequest(this.img_data[this.current_image])); 
     return; 
    }// end function 

    private function next_image_loaded(event:Event) 
    { 
     var _loc_2:Number = 0; 
     this.new_img = event.target.content; 
     stage.addChild(this.new_img); 
     if (this.strapline) 
     { 
      stage.setChildIndex(this.strapline, (stage.numChildren - 1)); 
     } 
     if (this.banner) 
     { 
      stage.setChildIndex(this.banner, (stage.numChildren - 1)); 
     } 
     if (this.button) 
     { 
      stage.setChildIndex(this.button, (stage.numChildren - 1)); 
     } 
     if (this.current_image == 0 && this.done != true) 
     { 
      this.button = new tech_button(); 
      this.button.x = 28; 
      this.button.y = 257; 
      //stage.addChild(this.button); 
      var _loc_3:Boolean = true; 
      this.button.buttonMode = true; 
      this.button.useHandCursor = _loc_3; 
      this.button.addEventListener(MouseEvent.CLICK, this.goToTechPage); 
      TweenLite.from(this.button, 0.5, {alpha:0, delay:1.5}); 
      this.banner = new bottom_banner(); 
      this.banner.x = 0; 
      this.banner.y = stage.stageHeight - 40; 
      stage.addChild(this.banner); 
      this.strapline = new strap(); 
      this.strapline.x = 232; 
      this.strapline.y = 195; 
      stage.addChild(this.strapline); 
      TweenLite.from(this.strapline, 1, {x:stage.stageWidth, ease:Expo.easeOut, delay:0.75, onComplete:this.fade_strap}); 
      this.done = true; 
     } 
     else 
     { 
      _loc_2 = 0.75; 
     } 
     TweenLite.from(this.new_img, 1, {autoAlpha:0, ease:Expo.easeOut, onComplete:this.swap_clips, delay:_loc_2}); 
     return; 
    }// end function 

    private function goToTechPage(event:MouseEvent) : void 
    { 
     navigateToURL(new URLRequest("report.htm"), "_self"); 
     return; 
    }// end function 

    private function fade_strap() 
    { 
     TweenLite.to(this.strapline.bg_mc, 1.5, {alpha:0.85}); 
     return; 
    }// end function 

    private function remove_strap() 
    { 
     this.strapline.parent.removeChild(this.strapline); 
     this.strapline = null; 
     return; 
    }// end function 

    private function swap_clips() 
    { 
     if (this.img) 
     { 
      stage.removeChild(this.img); 
     } 
     this.img = this.new_img; 
     this.new_img = null; 
     setTimeout(this.next_content, 6500); 
     return; 
    }// end function 

    private function err(event:IOErrorEvent) 
    { 
     if (this.img_data) 
     { 
      this.next_content(); 
     } 
     return; 
    }// end function 

} 

}

+2

爲什麼你註釋掉'stage.addChild(this.button);'?我期望,因爲按鈕被分配,這一個是錯誤的行。 – Vesper

回答

1

這是一個很大的代碼,有人要讀通過,您可以縮小其中的錯誤是在重點用trace("trace 1")發生點你的代碼(每次更改數字)。當你執行你的代碼的輸出窗口將顯示你的痕跡,即:

trace 1 
trace 2 
trace 3 
ArgumentError: Error #2025: The supplied DisplayObject must be a child of the caller 

這告訴你第三跟蹤之後發生的錯誤一段時間。你得到的錯誤告訴你它正在next_image_loaded函數中發生,所以請嘗試在那裏運行一些痕跡,並用結果更新你的文章。

3
//stage.addChild(this.button); 

似乎 '按鈕' 沒有被添加到載物臺(在next_image_loaded()方法),因此

if (this.button) 
{ 
    stage.setChildIndex(this.button, (stage.numChildren - 1)); 
} 

引發錯誤,雖然 '按鈕' 可以存在(!= NULL) ,它不添加到舞臺

嘗試用if語句如下:

if(button != null && button.stage != null) 

編輯:作爲維斯珀已經POIN在此期間同步

相關問題