无法从AS3中的Document Class访问静态var

时间:2012-06-17 03:14:02

标签: actionscript-3 static document root

我有一个名为“CityModule”的Document类,以及一个名为“City”的资产。以下是每个的系数。出于某种原因,我无法从CityModule访问City类的静态变量:

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的实例,我可以完全看到任何公共/ getter / setter。但静态不起作用......

1 个答案:

答案 0 :(得分: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();
        }

    }

}