3 Replies Latest reply on Apr 23, 2007 12:03 PM by Newsgroup_User

    initiate new Class with eval?

    Level 7
      Hi,

      I'm working on a piece of software which has a few different gametypes.
      The type of the game that needs to be initiated is read from xml, and
      after that, a _global is filled with a new instance of that type of
      game. At the moment the script looks like this:

      gameType = "2";
      switch (gameType) {
      case "1" :
      _global.theGame = new gameType1();
      break;
      case "2" :
      _global.theGame = new gameType2();
      break;
      case "3" :
      _global.theGame = new gameType3();
      break;
      }

      Of course this works, but I'd like to have it a bit more flexible. This
      way, everytime I create a new game type, I have to add a line to the
      switch. I've tried the two following lines:

      eval("_global.theGame = new gameType"+gameType+"();");
      _global.theGame = eval("new gameType"+gameType+"()");

      But they don't work. Is there a way to script something like this?

      Eric
        • 2. Re: initiate new Class with eval?
          Level 7
          Hmmm, doesn't work either... meanwhile I've tried:

          _global.theGame = eval("gameType"+gameType)();
          _global.theGame = new eval("gameType"+gameType)();
          _global.theGame = eval("new gameType"+gameType+"()");
          _global.theGame = new eval("gameType"+gameType+"()");
          eval("_global.theGame = new gameType"+gameType+"();");

          I guess it's just impossible :S

          E

          kglad wrote:
          > try:
          >
          >
          >
          > _global.theGame=eval("gameType"+gameType)();
          • 3. Re: initiate new Class with eval?
            Level 7
            Classes are stored within _global. To gain a class reference through eval,
            you would need to use

            class = eval("_global.ClassName");
            // or
            class = eval("_global.package.ClassName");

            Use of array access operators is recommended over eval. For that you would
            use:

            class = global["ClassName"];
            // or
            class = _global["package"]["ClassName"];

            You will, however, have to make sure that the class was compiled into your
            SWF. If the class not associated with a movie clip symbol exported for the
            first frame of your movie (or that symbol exists somewhere in your movie) or
            the class is not referenced by its full name in code and not by a string
            name, this will not happen. Then you would need to call it out by name to
            force its inclusion in the SWF. For that, all you need to do is use the
            class name somewhere in your code.

            ClassName;
            // or in your case
            gameType1;
            gameType2;
            gameType3;

            This lets Flash recognize that the class is used by the SWF and will compile
            the class into the SWF during compile.

            "Eric Adriaans" <forum@macromedia.com> wrote in message
            news:f0cp8n$8mo$1@forums.macromedia.com...
            > Hi,
            >
            > I'm working on a piece of software which has a few different gametypes.
            > The type of the game that needs to be initiated is read from xml, and
            > after that, a _global is filled with a new instance of that type of game.
            > At the moment the script looks like this:
            >
            > gameType = "2";
            > switch (gameType) {
            > case "1" :
            > _global.theGame = new gameType1();
            > break;
            > case "2" :
            > _global.theGame = new gameType2();
            > break;
            > case "3" :
            > _global.theGame = new gameType3();
            > break;
            > }
            >
            > Of course this works, but I'd like to have it a bit more flexible. This
            > way, everytime I create a new game type, I have to add a line to the
            > switch. I've tried the two following lines:
            >
            > eval("_global.theGame = new gameType"+gameType+"();");
            > _global.theGame = eval("new gameType"+gameType+"()");
            >
            > But they don't work. Is there a way to script something like this?
            >
            > Eric