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();
Inspiring
June 29, 2006
Most probably because the class definition is not registered in the _global
namespace.

Simple (but bloody strange) solution:

// add these statements somewhere in the script -- they will force the
compiler to load the class definitions
// note: must be fully qualified class paths
game1;
game2;
// etc...

var str = 'game1';
var f = eval(str);
var currentGame = new f();

"=Slayer=" <webforumsuser@macromedia.com> wrote in message
news:e7v82i$n3l$1@forums.macromedia.com...
> 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?


_Slayer_Author
Inspiring
June 29, 2006
Thanks for the help! I tried out the last suggestion (adding game1; game2; and so on) and this time around, the object is created correctly... but for some reason the game does not run still. But that is most likely an error in some other part of the code, so I'll have to look into that. I didn't quite understand what you meant by "note: must be fully qualified class paths" , maybe that's what I did wrong...

Pem

PS I won't have access to my computer for the next few days, so if someone replies to this, I might not reply until around monday.