Skip to main content
Inspiring
June 28, 2006
Question

String in constructor call

  • June 28, 2006
  • 4 replies
  • 461 views
I have a variable currentGame which points to a blank Object. A random game is chosen from a pool of many games. I want to then call the right constructor based on the game that was chosen, for example if game1 is chosen, I want my constructor to say currentGame = new game1(); if game 2 was chosen it should be currentGame = new game2();

Each game takes no arguments in the constructor. I was wondering if it is possible to incorporate a string into the constructor call. If it's not I'll just have to make a very large switch statement... but it's always good to know if there's an easier way =]

Thanks
Pem
This topic has been closed for replies.

4 replies

June 30, 2006
If it includes it, it will increase the file size, correct? So, when it compiles it only checks actual uses of the class, not strings - because it doesn't know to include those. That'd be my guess.
_Slayer_Author
Inspiring
July 3, 2006
I found the mistake that was causing the game not to work, it works fine now with the code you mentioned. Thank you all for your help!

Pem
Inspiring
June 30, 2006
Lol. Good to know someone else finds it odd to have to do that. It'd be nice
to have some kind of preprocessor statement to force the compiler to include
classes.


"NSurveyor" <saif7463@yahoo.com> wrote in message
news:e82dpn$m50$1@forums.macromedia.com...
> Ah yes, I remember that. I had to do that in order to have a dynamic
chosice
> for the easing in a Tween:
>
http://groups.google.com/group/macromedia.flash.actionscript/browse_thread/threa
>
d/4e490b22af91600d/c1118efa06688630?lnk=st&q=author%3ANSurveyor&rnum=1#c1118
efa0
> 6688630
>


June 30, 2006
June 28, 2006
Perhaps something like:

var str = 'game1';
var f = eval(str);
currentGame = new f();
_Slayer_Author
Inspiring
June 29, 2006
When I use that code, it doesn't work. The debugger says that currentGame is undefined after currentGame = new f(); is executed. Are you sure this should work?
Inspiring
June 29, 2006
Amazingly that would work if you could type the var currentGame:
var str = 'game1';
var f = eval(str);
var currentGame:game1 = new f();
But obviously you can't. And you can't use:
var currentGame:f

An option to solve this is to use a class 'GameEngine' that listens to custom events 'game1', 'game2'. Don't know if that's feasible within your app.