2011-01-21 40 views
0

當我從庫中將一個對象添加到時間線中的舞臺(通過將腳本放入時間線中)它可以工作,但是當我嘗試從此.as文件添加它時什麼都沒發生。addChild()從外部.as文件登臺

package com.wld.utils { 
    import flash.display.MovieClip; 

    public class ISGallery extends MovieClip { 
     var imageArray:Array = new Array(); 

     public function ISGallery() { 

     } 

     public function addImageURL(imageURL:String):void { 
      imageArray.push(imageURL); 
      var gallerythumb:ISGalleryThumb = new ISGalleryThumb(); 
      addChild(gallerythumb); // nothing happens 
     } 
    } 
} 

謝謝。

+1

您是否創建ISGallery的實例並將其添加到舞臺的任何位置? – Alex 2011-01-21 01:41:28

回答

1

首先你添加對象的函數只是一個函數;

public function test() 
{ 
//addcode here 
} 

然後咕回你的主類,並導入「com.wld.utils.ISGallery」

package 
{ 

    import com.wld.utils.ISGallery; 

,現在你在你的主代碼創建一個使用主類外部的一個變種coode。

var myExternalClass:ISGallery = new ISGallery(); 

最後你必須調用,它是將對象, 功能,然後添加新的類。

addChild(myExternalClass);  
myExternalClass.hello(); 

現在爲例。

洙這裏有那麼主要的代碼應該是什麼樣子:

package 
{ 
    import flash.display.*; 
    import flash.text.*; 
    import flash.events.*; 
    import flash.ui.*; 
    import flash.utils.*; 
    import flash.media.*; 
    import com.wld.utils.ISGallery; 
    public class Test extends MovieClip 
    { 
     public function Test() 
     { 
      var myExternalClass:ISGallery = new ISGallery(); 

      addChild(myExternalClass); 
      myExternalClass.hello(); 
     } 
    } 
} 

現在的外部類:

(注:擴展Sprite也可影片剪輯)

package com.wld.utils 
{ 
    import flash.display.*; 
    import flash.text.*; 
    import flash.events.*; 
    import flash.ui.*; 
    import flash.utils.*; 
    import flash.media.*; 
    public class ISGallery extends Sprite 
    { 
    var myFirstText:TextField = new TextField(); 
    var mySecondText:TextField = new TextField(); 
     public function ISGallery() 
     { 
     } 
     public function hello() 
     { 
      myFirstText.text = "test"; 
      mySecondText.y = 40; 
      mySecondText.text = "hello"; 
      addChild(myFirstText); 
      addChild(mySecondText); 
     } 
    } 
} 

在最終產品應該是這樣的。

輸出應該有一個短信說「測試」

上下等約1/4的方式,說「你好」。