7 Replies Latest reply: Jun 20, 2012 5:29 AM by Ned Murphy RSS

    Creating new instance using String name

    rjoshicool Community Member

      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?

        • 1. Re: Creating new instance using String name
          Ned Murphy MVP

          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 Community Member

            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 MVP

              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 Community Member

                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 MVP

                  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 Community Member

                    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 MVP

                      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.