0

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

CityModule.as:

package {

  public class CityModule extends MovieClip {

      public function CityModule() {

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

      }
    }
  }
}

城市.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/getter/setter。但是静态不起作用...

4

1 回答 1

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();
        }

    }

}
于 2012-06-17T03:33:29.533 回答