views:

51

answers:

3

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( _assetGenerator, _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();
                    }
                }
A: 

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*/);
Amarghosh
the argument inside the getDefinitionByName you describe is not a String and it's not in the format that Ender above indicates. What is the correct approach?
Ponty
@Ponty oops.. I forgot to quote it. The syntax is `pack.pack1.ClassName` [Check the documentation](http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/flash/utils/package.html#getDefinitionByName%28%29)
Amarghosh
ok thanks i have checked the documentation but i forgot the reference
Ponty
@Ponty Did you try the `pack::classname` syntax - is it working?
Amarghosh
sorry for late answering (small holidays :) ).Yes it worked with that format.Thanks
Ponty
A: 

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.

9re
i put a snippet of my code in my original post and as you can see i have put the import but i cannot get the class instance. I test localy for now
Ponty
Thanks, man! I can explain what happend.The class "factory.scratchers.ExplodeScratcher" has never appeared before, and not defined in the current ApplicationDomain.To add this in the current ApplicationDomain.1. Add var declaration : var c:factory.scratchers.ExplodeScratcherthe declaration can be either global or local (in the same scope).2. execute registerClassAlias before getDefinitionByName()registerClassAlias("factory.scratchers.ExplodeScratcher", ExplodeScratcher);3. define an array or object, which contains the reference of the class which you want to get the definition
9re
+1  A: 

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;
Ender
if i just import the whole package of the classes that i want to get their instance like i show in the original post, is it right? but i don't get the instance of the desired class...
Ponty
so just importing is not enough i have to put var MyClass:ClassName and worked! thanks
Ponty