|
-
Dance Monkey Dance!
getDefinitionByName problems
I'm having a couple problems with getDefinitionByName. What im trying to do is the following.
I load in a XML file with several items in an inventory which contain a couple details.
Eg
PHP Code:
<items>
<item>
<description>A simple hat</description>
<classname>Hat</classname>
</item>
<item>
<description>A pair of pants</description>
<classname>Pants</classname>
</item>
</items>
Then in my code I try to make an instance of these classes by using the following
PHP Code:
var ClassReference:Class = getDefinitionByName() as Class;
var item:Item = (new ClassReference as Item);
The problem I'm getting is an error saying
ReferenceError: Error #1065: Variable Hat is not defined.
Now I got around this problem by adding the following right before my script
var hat:Hat;
But this seems like a really bad hack since I could have thousands of different items. So having to place a list of them before this line seems a bit overkill.
Any ideas why this problem occurs?
Thanks
"I layed down in my bed last night looked up at the stars, and thought to myself... Where the F*#K is my roof"
-
Dance Monkey Dance!
Hey guys, I've been trying to play around with this all weekend does anyone have an idea of how to get it working?
"I layed down in my bed last night looked up at the stars, and thought to myself... Where the F*#K is my roof"
-
there's no real "solution" - but you can just not type it at all (or use Object or * - all of which are functionally equivalent):
PHP Code:
var ClassReference:Class = getDefinitionByName() as Class; var item:Object = new ClassReference();
-
If I understand it right, the problem is not with typing, but rather that the compiler will not include in the library any classes which are not explicitly used.
To get around this, you either must have a reference to the class like you did by declaring a var of that type, or you must have an instance of that class somewhere. What you can do is create a library swf with all of these classes and an instance of each dragged onto the stage. Load that swf and get the classes from it by name.
Edit: also, your code is missing an argument to getDefinitionByName. I assume that's an oversight when typing it here.
-
Dance Monkey Dance!
5TonsOfFlax: Yeah that missing argument is a typo Yeah I've been searching around and it seems your right. At least one instance needs to have been created. Also I have the class imported in the script I'm using this in do I need the class path in the string?
eg
PHP Code:
String class = "Hat"; var ClassReference:Class = getDefinitionByName("com.mysite.items."+class) as Class;
moagrius: I didn't think of just using a standard object. I'll give it a shot and see how it goes.
Thanks for the replies, I appreciate it.
"I layed down in my bed last night looked up at the stars, and thought to myself... Where the F*#K is my roof"
-
You should not need the fully qualified classname if the code it's executing from is in the same package. You may not need it at all unless there are multiple Hats.
You also do not need an instance of the class, but you do need a reference to that class to be used in the code. You could simply declare a variable of that type and never instantiate it. But creating an instance works fine as well. Basically, you have to convince the compiler that that class definition is necessary to the final swf.
-
the short answer is: if you're going to use those classes, they need to be compiled in the swiff - at compile-time, not run-time. so, no, you can't get around having to reference all the classes you want to dynamically generate.
i suspect the quickest way would be to just reference the classes you want to use in a list, in the document class, like so:
PHP Code:
private static var classes:Array = [Hat, Boots, Shirts, etc...];
-
Dance Monkey Dance!
Ahh that seems pretty smart, because I was thinking man I could have hundreds of these classes. It will suck having,
var shirt:Shirt;
var shirt2:Shirt2;
var shirt3:Shirt3;
eg
I'll give it a shot. Thanks moagrius, Thanks 5TonsOfFlax
"I layed down in my bed last night looked up at the stars, and thought to myself... Where the F*#K is my roof"
-
you don't need to do individual declarations - the list i mentioned above will suffice:
PHP Code:
var classes:Array = [Shirt, Shirt2, Shirt3, Hat, etc...];
still not ideal, i know - but a little cleaner.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|