3 Replies Latest reply on Dec 13, 2006 9:10 AM by injpix

    Dynamically initiating a class

    injpix Level 3
      Is it possible to dynamically initiate a class in AS2? The failed code below will hopefully highlight what I am trying to do.

      var currentClass = “Ball”;
      var myBall = new [currentClass]();
      trace(myBall);//undefined
        • 1. Re: Dynamically initiating a class
          Peter Lorent Level 2
          You get undefined because the var myBall isn't typed. Try:

          Ball;
          var currentClass:String="Ball";
          var myBall=new [currentClass]();
          trace(myBall);

          Mind the first line of code. This forces the compiler to include the class Ball.
          • 2. Re: Dynamically initiating a class
            injpix Level 3
            Wow, you’re good LuigiL! That works great! Thanks again.
            • 3. Re: Dynamically initiating a class
              injpix Level 3
              I am still somewhat dumfounded by this. Would you perhaps know how this is working? Here are my thoughts on it and please correct me if you wish;

              Ball;
              This line of code implicitly imports the Ball class. If this is so I am not sure how the compiler would know that there would be a class ‘Ball’ and not treat it as a variable with a different datatype?

              var currentClass:String="Ball";
              Declare currentClass as a String datatype.

              var myBall=new [currentClass]();
              And here is my shot in the dark, since ActionScript is ECMA-262 compliant it can multiple Strings and Numbers together. So it doesn’t see a difference between datatypes in some operations. I am assuming this is happening with my case.

              Any comments?