2012-06-17 93 views
0

我有一個名爲「CityModule」的Document類和類爲「City」的資產。下面是每個係數。出於某種原因,我無法從CityModule訪問城市類的靜態變量:無法從AS3中的文檔類訪問靜態變量

CityModule.as:

package { 

    public class CityModule extends MovieClip { 

     public function CityModule() { 

      var buildings:Array = City.getBuildings(); //gives error 

     } 
    } 
    } 
} 

City.as:

package { 

import flash.display.MovieClip; 

public class City extends MovieClip { 

    private static var _buildings:Array = [ 
     {className:'City.Generic1', type:'generic'}, 
     {className:'City.Generic2', type:'generic'}, 
     {className:'City.Generic3', type:'generic'} 
    ]; 

    public function City(){ 
     //empty 
    } 

    public static function getBuildings():Array{ 
     return _buildings; 
    } 
    } 
} 

這樣做給了我一個「調用可能未定義的方法getBuildings」錯誤。如果我實例化City的一個實例,我可以看到任何public/getters/setters都很好。但靜態不工作...

回答

0

您在CityModule中缺少MovieClip導入。

下面的代碼在Flash Develop中爲我編譯。

//CityModule.as 
package 
{ 
    import flash.display.MovieClip; 
    public class CityModule extends MovieClip 
    { 
     public function CityModule() 
     { 
      var buildings:Array = City.getBuildings(); //gives error 
     } 
    } 
} 

//City.as 
package { 

import flash.display.MovieClip; 

public class City extends MovieClip { 

    private static var _buildings:Array = [ 
     {className:'City.Generic1', type:'generic'}, 
     {className:'City.Generic2', type:'generic'}, 
     {className:'City.Generic3', type:'generic'} 
    ]; 

    public function City(){ 
     //empty 
    } 

    public static function getBuildings():Array{ 
     return _buildings; 
    } 
    } 
} 


//Main.as 
package 
{ 
    import flash.display.Sprite; 
    import flash.events.Event; 

    public class Main extends Sprite 
    { 

     public function Main():void 
     { 
      if (stage) init(); 
      else addEventListener(Event.ADDED_TO_STAGE, init); 
     } 

     private function init(e:Event = null):void 
     { 
      removeEventListener(Event.ADDED_TO_STAGE, init); 
      // entry point 

      var c:CityModule = new CityModule(); 
     } 

    } 

} 
+0

嗯,我已經把它放進去,但問題修正了它自己?這可能是我用Flash Pro安裝Flash Builder的方式,我似乎有時會得到隨機錯誤,然後在被篡改後消失。 –