Saturday, October 27, 2007

How to call a class that you haven't imported.

There was a post on the FlashCodersNY mailing list that was asking about how to access code that you dont have scope to. There are a few ways to do this but in general if you are having this problem it might be a sign that you application is not build the way that it should be. Unless you are doing something dynamic I suggest passing scope or importing an actual reference.

Step 1,
Import your class and make a reference to it. The import statement itself will not compile your class into your swf. If you never reference it, the compiler will simply take it out to make your swf smaller.

package {
import com.project.Class1; // your import
public class Main {
private var class1:Class1; // the reference
}
}

Step 2,
In a different class you can then get access to Class1 by using getDefinition();
var class1:Class = ApplicationDomain.currentDomain.getDefinition( "com.project.Class1" ) as Class;   

Here is a full example, source code can also be downloaded here.
// Document class
package {
import flash.display.Sprite;
import com.project.Class1; // we will import this here
import com.project.Class2;
public class Main extends Sprite {
private var class1:Class1; // And then give reference to it so that this class is compiled into the swf
public function Main() {
var class2:Class2 = new Class2();
}
}
}

// class that is not included in
package com.project {
public class Class1 {
public function Class1() {
trace("class1");
}
}
}

package com.project {
import flash.system.ApplicationDomain;
public class Class2 {
public function Class2() {
var class1:Class = convertStringToClass("com.project.Class1");
new class1();
trace("class2");
}
public function convertStringToClass( className:String ) : Class {
return ApplicationDomain.currentDomain.getDefinition( className ) as Class;
}
}
}


You dont need to use ApplicationDomain to make this work but you can learn more why you might need this from the links below.

I've also added links to other similar actionScript 3 methods with similar function.

About ApplicationDomain
http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/flash/system/ApplicationDomain.html#getDefinition()
and a little more
http://livedocs.adobe.com/flash/9.0/main/wwhelp/wwhimpl/common/html/wwhelp.htm?context=LiveDocs_Parts&file=00000327.html

and other package.utils functions
http://livedocs.adobe.com/flex/2/langref/flash/utils/package.html

getDefinition()
Gets a public definition from the specified application domain. The definition can be that of a class, a namespace, or a function.

describeType()
Produces an XML object that describes the ActionScript object named as the parameter of the method. This method implements the programming concept of reflection for the ActionScript language.

getDefinitionByName()
Returns a reference to the class object of the class specified by the name parameter.

getQualifiedClassName()
Returns the fully qualified class name of an object.

getQualifiedSuperclassNam()
Returns the fully qualified class name of the base class of the object specified by the value parameter.

0 Comments:

Post a Comment

Links to this post:

Create a Link

<< Home