Actionscript 3 从运行时加载的swf实例化类时出错
我有以下资料:Actionscript 3 从运行时加载的swf实例化类时出错,actionscript-3,class,flash,external,Actionscript 3,Class,Flash,External,我有以下资料: protected function caller(event:FlexEvent):void { var r:URLRequest=new URLRequest('http://remote/TESTLibrary.swf'); var c:LoaderContext=new LoaderContext(); c.applicationDomain=ApplicationDo
protected function caller(event:FlexEvent):void
{
var r:URLRequest=new URLRequest('http://remote/TESTLibrary.swf');
var c:LoaderContext=new LoaderContext();
c.applicationDomain=ApplicationDomain.currentDomain;
var l:Loader=new Loader();
l.contentLoaderInfo.addEventListener(Event.COMPLETE, hndComplete);
l.load(r, c);
}
protected function hndComplete(event:Event):void
{
var d:ArrayCollection; //not used but here for
var cls:Class=(event.target as LoaderInfo).applicationDomain.getDefinition('ro.vnc.modules.ModuleManager') as Class;
var instance:Object=new cls();
}
以及库中包含的类:
package ro.vnc.modules
{
import mx.collections.ArrayCollection;
public class ModuleManager
{
public function ModuleManager()
{
var d:ArrayCollection;//if commented works fine
var c:Number=5;
trace('ModuleManager', c);
}
}
}
如果我对d:ArrayCollection的定义进行注释,一切都正常,但我使用了全局可访问包中的类,如mx.collections,我会得到一个VerifyError:Error#1014:Class mx.collections::ArrayCollection找不到。非常感谢您的帮助。问题在于您正在引用ArrayCollection,而没有首先在调用者中导入它。我完全知道您要做什么——在运行时将该类拉入标准使用,但flash不允许这样做。没有对类的显式引用,flash被迫猜测“ArrayCollection”是什么——因此它在编译时抛出一个验证错误。我尝试了几种不同的类类型及其标准行为 注意-如果在被调用方中实例化该类,则可以在调用方中获得对正确类型对象的引用:
package ro.vnc.modules
{
import mx.collections.ArrayCollection;
public class ModuleManager
{
public var d:ArrayCollection;
public function ModuleManager()
{
d = new ArrayCollection();
var c:Number=5;
trace('ModuleManager', c);
}
}
}
但是,一旦您在调用者中引用它作为其类型,就会得到一个验证错误
protected function hndComplete(event:Event):void
{
var cls:Class=(event.target as LoaderInfo).applicationDomain.getDefinition('ro.vnc.modules.ModuleManager') as Class;
var instance:Object=new cls();
trace(instance.d); //[object ArrayCollection]
var d1:ArrayCollection = instance.d; // throws verify error
var d2:* = (event.target as LoaderInfo).applicationDomain.getDefinition(instance.d); // throws reference error
}
解决方案是在调用类中导入类型。
(尽管我希望有人能为这个场景想出一个解决方案,在没有额外导入的情况下运行…)
干杯 问题在于您正在引用ArrayCollection,而没有首先在调用方中导入它。我完全知道您要做什么——在运行时将该类拉入标准使用,但flash不允许这样做。没有对类的显式引用,flash被迫猜测“ArrayCollection”是什么——因此它在编译时抛出一个验证错误。我尝试了几种不同的类类型及其标准行为 注意-如果在被调用方中实例化该类,则可以在调用方中获得对正确类型对象的引用:
package ro.vnc.modules
{
import mx.collections.ArrayCollection;
public class ModuleManager
{
public var d:ArrayCollection;
public function ModuleManager()
{
d = new ArrayCollection();
var c:Number=5;
trace('ModuleManager', c);
}
}
}
但是,一旦您在调用者中引用它作为其类型,就会得到一个验证错误
protected function hndComplete(event:Event):void
{
var cls:Class=(event.target as LoaderInfo).applicationDomain.getDefinition('ro.vnc.modules.ModuleManager') as Class;
var instance:Object=new cls();
trace(instance.d); //[object ArrayCollection]
var d1:ArrayCollection = instance.d; // throws verify error
var d2:* = (event.target as LoaderInfo).applicationDomain.getDefinition(instance.d); // throws reference error
}
解决方案是在调用类中导入类型。
(尽管我希望有人能为这个场景想出一个解决方案,在没有额外导入的情况下运行…)
干杯 本例中的类型是导入的,如果您更加注意旧的加载程序,可以看到这一点。不过,我写了另一种加载swf的方法,效果非常好:
public function ModulesInstaller()
{
var f:File=new File();
f.addEventListener(Event.SELECT, hndSelect);
f.browseForOpen('Select Library', [new FileFilter('Library file', '*.swf')]);
}
protected function hndSelect(event:Event):void
{
var fs:FileStream=new FileStream();
fs.open(event.target as File, FileMode.READ);
var bytes:ByteArray=new ByteArray();
fs.readBytes(bytes);
var lc:LoaderContext=new LoaderContext();
lc.allowCodeImport=true;
var l:Loader=new Loader();
l.contentLoaderInfo.addEventListener(Event.INIT, hndLoaded);
l.loadBytes(bytes, lc);
l.loaderInfo
}
private function hndLoaded(event:Event):void
{
var clsRef:Class=(event.target as LoaderInfo).applicationDomain.getDefinition('classRefName') as Class;
}
本例中的类型是导入的,如果您更加关注旧的加载程序,可以看到这一点。不过,我写了另一种加载swf的方法,效果非常好:
public function ModulesInstaller()
{
var f:File=new File();
f.addEventListener(Event.SELECT, hndSelect);
f.browseForOpen('Select Library', [new FileFilter('Library file', '*.swf')]);
}
protected function hndSelect(event:Event):void
{
var fs:FileStream=new FileStream();
fs.open(event.target as File, FileMode.READ);
var bytes:ByteArray=new ByteArray();
fs.readBytes(bytes);
var lc:LoaderContext=new LoaderContext();
lc.allowCodeImport=true;
var l:Loader=new Loader();
l.contentLoaderInfo.addEventListener(Event.INIT, hndLoaded);
l.loadBytes(bytes, lc);
l.loaderInfo
}
private function hndLoaded(event:Event):void
{
var clsRef:Class=(event.target as LoaderInfo).applicationDomain.getDefinition('classRefName') as Class;
}
本例中的类型是导入的,如果您更加关注旧的加载程序,可以看到这一点。然而,我编写了另一种加载swf的方法,这种方法非常有效:我没有在旧的加载程序中看到导入语句(尽管它可能在那里,但没有发布)。很明显,我在你加载的模块中看到了。。。只是出于兴趣-如何在模块中建立“classRefName”?很高兴你找到了“按类”的方法。干杯本例中的类型是导入的,如果您更加关注旧的加载程序,可以看到这一点。然而,我编写了另一种加载swf的方法,这种方法非常有效:我没有在旧的加载程序中看到导入语句(尽管它可能在那里,但没有发布)。很明显,我在你加载的模块中看到了。。。只是出于兴趣-如何在模块中建立“classRefName”?很高兴你找到了“按类”的方法。干杯