View Full Version : Does Class Exist?
pardoner
September 8th, 2009, 02:22 AM
Is there a way to see if a class exists? something like:
if( MyClass exist in the Library or has been imported in to the class ){
var myClass = new MyClass()
addChild(myClass);
}else{
trace("MyClass doesn't exist")
}
I'm not trying to see if it exists on the stage just want to know if I have access to it.
JonnyR
September 8th, 2009, 02:47 AM
Pardoner,
I'm not sure I really understand what you are asking for. If you try to perform
var myClassInstance : Object = new MyClass();
And the MyClass Class Definition does not exist, the compiler (MXMLC) will refuse to compile the project, no?
Jonny.
Anogar
September 8th, 2009, 02:59 AM
Yeah, I'm that'll throw a compile-time error. You can only catch runtime errors.
theCodeBot
September 8th, 2009, 06:48 AM
I think you might want to look at getDefinitionByName -- search for that on these forums, you'll find a lot of different posts where it came in useful, most of them possibly related to what you're trying to do here.
pardoner
September 8th, 2009, 09:58 AM
Thanks CodebBot I tried GetDefinitionByName('MyClass') but that gives me a compiler error:
ReferenceError: Error #1065: Variable MyClass is not defined.
:(
senocular
September 8th, 2009, 10:10 AM
Thanks CodebBot I tried GetDefinitionByName('MyClass') but that gives me a compiler error:
ReferenceError: Error #1065: Variable MyClass is not defined.
:(
No, that's a runtime error - the error that answers your question: No, you do not have access to MyClass; it does not exist. This explains the error text, "MyClass is not defined." ;)
IQAndreas
September 8th, 2009, 10:27 AM
Here is the language reference on it:
http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/flash/utils/package.html#getDefinitionByName%28%29
Don't forget to include the package name:
getDefinitionByName("com.mypackage.MyClass");
You could also check out the ApplicationDomain.getDefinition() function.
http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/flash/system/ApplicationDomain.html#getDefinition%28%29
This might work. Copy and paste the exact error if it doesn't.
function getClass(name:String):Class
{
try
{
return ApplicationDomain.currentDomain.getDefinition(name );
}
catch (error:ReferenceError)
{
trace("Class", name, "not found. Returning null");
return null;
}
catch (error:Error)
{
trace("Unknown error:", e.toString());
}
}
//Example usage
var className = "flash.display.MovieClip";
var definition:Class = getClass(className);
if (definition != null)
{
var newObject:definition = new definition();
//Then some extra fluff. :)
if (newObject is DisplayObject)
{
newObject.x = stage.stageWidth / 2;
newObject.y = stage.stageHeight / 2;
this.addChild(newObject);
}
}
thatsasif
September 8th, 2009, 10:48 AM
This also worked for me.
try {
if (MyClass as Class) {
trace("yes");
}
} catch (error:Error) {
trace("Class not found");
}
wvxvw
September 8th, 2009, 11:11 AM
ApplicationDomain.currentDomain.hasDefinition(name );
. . .
theCodeBot
September 8th, 2009, 11:20 AM
ActionScript Code:
ApplicationDomain.currentDomain.hasDefinition(name );
. . .
Worth noting that ApplicationDomain is Flex-only ;)
senocular
September 8th, 2009, 11:26 AM
Worth noting that ApplicationDomain is Flex-only ;)
No its not ;)
pardoner
September 8th, 2009, 11:34 AM
Sorry thatsasif that doesn't work. iqAndreas that worked with a few modifications. Here's what I ended up with.
var definition:Class = _getClass("myClass");
if (definition != null) {
var newObject:* = new definition();
//Then some extra fluff. :)
if (newObject is DisplayObject)
{
newObject.x = stage.stageWidth / 2;
newObject.y = stage.stageHeight / 2;
addChild(newObject);
}
}
}
private function _getClass(name:String):Class
{
try {
return ApplicationDomain.currentDomain.getDefinition(name ) as Class;
} catch (error:ReferenceError) {
trace("Class", name, "not found. Returning null");
} catch (error:Error) {
trace("Unknown error:", error.toString());
}
return null;
}
Powered by vBulletin® Version 4.1.10 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.