-
1. Re: Creating new instance using String name
Ned Murphy Jun 19, 2012 4:14 AM (in response to rjoshicool)For each class use:
var ClassRef:Class = Class(getDefinitionByName("Screen1"));
var screen1:* = new ClassRef();
addChild(screen1);
I show "Screen1" only because the way you show it does not make sense to have done as written code. For what you showed, the 1,2 would normally be a variable, in which case you would add them cast as a String, as in: "Screen"+String(num)
Also, you will run into a problem if you declare multiple variables using the same variable name (var screen)
-
2. Re: Creating new instance using String name
rjoshicool Jun 19, 2012 6:34 AM (in response to Ned Murphy)I used this but get an error:
ReferenceError: Error #1065: Variable Screen1 is not defined. at global/flash.utils::getDefinitionByName()
-
3. Re: Creating new instance using String name
Ned Murphy Jun 19, 2012 7:40 AM (in response to rjoshicool)Did you create a class named Screen1? You need to have the Screen1 class created/accessible. Just naming it in that code does not make it exist.
-
4. Re: Creating new instance using String name
rjoshicool Jun 20, 2012 3:03 AM (in response to Ned Murphy)I did the following:
import com.rjdesignz.myapp.slides.Slide1; . . . private var Slide1:Class; . . . var slide:*; switch(num) { case 1: var ClassRef:Class = Class(getDefinitionByName("Slide1")); slide = new ClassRef(); break;
But I get the same error:
ReferenceError: Error #1065: Variable Slide1 is not defined.
at global/flash.utils::getDefinitionByName()
-
5. Re: Creating new instance using String name
Ned Murphy Jun 20, 2012 4:29 AM (in response to rjoshicool)You do not want to be using the line:
private var Slide1:Class;
The
var ClassRef:Class = Class(getDefinitionByName("String1")
line does the dynamic creation.
If your code can stand with using "Sprite1", then you do not need the code I provided. You can just use:
var sprite1:Sprite1 = new Sprite1();
You would only be using the code I provided if you are dynamically defining which class needs to be instantiated. What I was saying in the first regarding your use of "1" in the code you started with is that if you know it is a "1" then you do not need to dynamically define the class. Normally that "1" would be a numeric variable (like " i "), such that the " i " might be any value.
-
6. Re: Creating new instance using String name
rjoshicool Jun 20, 2012 4:44 AM (in response to Ned Murphy)I earlier used it without writing:
private var Slide1:Class;
But got the same error.
Now in the above code, I have removed this line and tried again, but get the same error. Yes, I am having multiple class files named: Slide1, Slide2 and so on. I need to instantiate them by calling something like: "Slide" + num, where num is a dynamic parameter I pass.
But I am getitng the same error of Variable not defined.
-
7. Re: Creating new instance using String name
Ned Murphy Jun 20, 2012 5:29 AM (in response to rjoshicool)I do not know what your code looks like now, and you now say you are getting a "Variable not defined" error, which is not the 1007 error you were getting earlier. What does your Sprite1 class look like?
You should go into your Flash Publish Settings and select the option to Permit Debugging. THat can help by identifying the specific line causing an error. You want to be sure you are chasing the right issue and not something unrelated.