开发者

getDefinitionByName works with custom classes?

I wonder if the flash.utils.getDefinitionByName(name:String):Class works with custom classes? Is there a problem if the class has a constructor that takes arguments? I tell this because i have a class in a package of the form packageA.packageB and when i input the name of the class into the above function as packageA.packageB.ClassName does not work. Also i have tried (with the same result) the output from getQualifiedClass which gives packageA.packageB::ClassName. Any ideas??

Here is part of the code that belongs to a file that imports like import factory.scratchers.*; all the scratch elements which have the names AlphaScratcher, DissolveScratcheer, ExplodeScratcher ,etc. Does the above import satisfies the requirement?

import factory.scratchers.*;
...
for ( var iArea:uint = 0; iArea < _totalScratchAreas; iArea++ ) {       
                var sourceArray:Array = new Array();
                var currentNameArray:Array = _globalAssetNameArray[iArea];
                var theScratcher:Scratcher;
                for ( var index:uint = 0; index < _globalMsgArray[iArea].length; index++ ) {
                    // here i would like to have something like:>
                    var ScratchClass:Class = getDefinitionByName( "factory.scratchers::ExplodeScratcher") as Class;
                    theScratcher = new ScratchClass( _assetG开发者_如何学编程enerator, _mainSprite );
                    // instead of: - but it does not work not sprite shown on screen
                    theScratcher = new ExplodeScratcher( _assetGenerator, _mainSprite );
                    theScratcher.setBack( currentNameArray[index] );                    
                    sourceArray.push( theScratcher );

                    if ( _globalArray[iArea][4] == OPENEDAREA ) {
                        theScratcher.auto();
                    }
                }


You should specify what "does not work" means. It's important whether it's a runtime or compile-time error.

Input to getDefinitionByName should always be a string, of the format "package.subpackage.subpackage::Class", so:

var MyClass:Class = getDefinitionByName("packageA.packageB::ClassName") as Class;
var myInstance:Object = new MyClass();

Note that Flash will not include ClassName in your movie unless it is specifically mentioned somewhere in your code. To avoid this, place this somewhere in your main section of code:

var MyClass:ClassName;


If you are linking the custom class, have you ever tried adding 'import'? Or if you are treating the custom class as runtime shared libraries, you can use ApplicationDommain.getDefinition() instead.


Yes, it does work with custom classes. Just make sure that the SWF is aware of the class by referring to it somewhere in the code so that its definition is included at compile time. If you are not using the class anywhere in your code, its definition won't be compiled to the SWF and hence you can't read it back. If you're using it somewhere, here's how to use it:

var t:Object;
var type:Class = getDefinitionByName("packageA.packageB.ClassName") as Class;
if(!type)
  trace("can't find the definition");
else
  t = new type(/* arguments to the constructor*/);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜