所以我正在使用flash进行项目,最近我决定将测试文件切换为项目中的主要类。该文件名为ColorClass.as,关联的.fla文件为ColorClass.fla。它们位于同一目录中,ColorClass.fla的Document类是ColorClass。我在ColorClass.as中使用加载器来加载外部SWF,如下所示:
public var loader:Loader = new Loader();
addChild(loader); //adding loader
loader.load(new URLRequest("../Resource/flash/WheelClasses.swf"));
loader.contentLoaderInfo.addEventListener(Event.COMPLETE,wheelsLoaded);
我正在尝试创建一个测试文件color_Test.as(和一个关联的color_Test.fla),它加载ColorClass.swf并获取要在测试文件中使用的类定义。
但是,当我尝试编译ColorClass.as/.fla并创建.swf文件时,我收到了多个
的实例Access of undefined property loader.
和
Call to a possibly undefined method addChild.
这些错误完全独立于color_Test。我在这里做错了什么吗?我只是尝试编译ColorClass.as/.fla,我可以在尝试将其更改为另一个类之前这样做。
答案 0 :(得分:1)
在AS3类中,所有功能代码都需要包含在函数中。
因此,除了第一行(这是一个变量声明)之外,其余的只是浮动在未包含在函数中的类中:
public var loader:Loader = new Loader(); //THIS LINE IS FINE
//THESE THREE LINES NEED TO LIVE IN A FUNCTION
addChild(loader);
loader.load(new URLRequest("../Resource/flash/WheelClasses.swf"));
loader.contentLoaderInfo.addEventListener(Event.COMPLETE,wheelsLoaded);
如果您希望立即执行上面的代码,请将其放在constructor函数(名称与类名称匹配的函数)中。
public function ColorClass {
addChild(loader); //adding loader
loader.load(new URLRequest("../Resource/flash/WheelClasses.swf"));
loader.contentLoaderInfo.addEventListener(Event.COMPLETE,wheelsLoaded);
}