Skip navigation
Currently Being Moderated

Creating new instance using String name

Jun 19, 2012 2:04 AM

Tags: #class #dynamic #instance

I am working on an application where multiple screens need to be loaded into the view. I have created these movieclips in Flash and linked each of them to classes. I have named these movieclips(or class names) as Screen1, Screen2, Screen3 etc. Now depending on the user input for screen number, I would like to create a new instance of that screen and load it into the view. I tried this:

 

private var Screen1:Class;
private var Screen2:Class;
.
.
.
.
.
.
var screen:MovieClip = new this["Screen" + 1];
var screen:MovieClip = new this["Screen" + 2];

 

But this doesn't works and gives me error:

 

TypeError: Error #1007: Instantiation attempted on a non-constructor.

 

Any idea how can I create these dynamic objects from the Class Name?

 
Replies
  • Currently Being Moderated
    Jun 19, 2012 4:14 AM   in reply 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)

     
    |
    Mark as:
  • Currently Being Moderated
    Jun 19, 2012 7:40 AM   in reply 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.

     
    |
    Mark as:
  • Currently Being Moderated
    Jun 20, 2012 4:29 AM   in reply 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.

     
    |
    Mark as:
  • Currently Being Moderated
    Jun 20, 2012 5:29 AM   in reply 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.

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points