2 Replies Latest reply on Jul 28, 2006 10:00 PM by WebXperience

    dynamically creating objects

    Level 7
      Does anyone know if you can dynamically create an object from the string
      name of the class?

      For example, if you have a class TestObj

      Instead of hard coding it like this:

      obj = new testObj();

      Could you do something like this:

      className = "TestObj";
      obj = new [className ]();

      or some such construct that would allow you to instaniate an a class on the
      fly?

      thanks


        • 1. Re: dynamically creating objects
          injpix Level 3
          That seems awkward to code like that. I suppose you can create a symbol in the library and have it access ‘TestObj’ then call it out onto the stage using attachMovie(). But it would seem that using a switch statement would even be more practicable for your request.
          • 2. Re: dynamically creating objects
            WebXperience Level 1
            The code for defining an ActionScript class exists in its own .as file.

            class Cube (
            var length:Number;
            var height:Number;
            var width:Number;
            functionCube(tempLength:Number, temHeight:Number, tempWidth:Number) {
            length = tempLength;
            height =tempheight;
            width = tempWidth;
            }
            function getVolume():Number {
            return length * height * width;
            }
            }
            After creating the class and saving it as an .as file in the same directory as your Flash authoring fie. You would create a new instance of the class in the following way:

            var myCube:Cube = new Cube(3, 2, 7);