A Flash Developer Resource Site

Results 1 to 10 of 10

Thread: [CS3] calling a dynamic class name... probably the wrong words (hence my problem)

  1. #1
    Senior Member Awoogamuffin's Avatar
    Join Date
    Nov 2008
    Posts
    208

    Question [CS3] calling a dynamic class name... probably the wrong words (hence my problem)

    Hi there,

    I've been looking around the internet for a solution to this problem, but I think I simply lack the terminology to find what I'm looking for. So instead of trying to put it into words, I'll give you exactly what my problem is.

    So each of my game levels is a class, called Level1, Level2 etc. (can you see the pattern?) which inherit from the class Level.

    Anyway, when you move to the next level, the game calls something like this:

    var level:Level = new Level1();

    now, obviously, if the player has completed level 1 I now want him to move on to level 2. So

    var level:Level = new Level2();

    At the moment, i'm doing this with a switch statement, which is just big and ugly and annoying. Is there now way I can do something Stringish, like this:

    var level:Level = new "Level"+nextLevelInt+"()"

    the switch statement works fine, but this is not the first time I've wished I could treat function calls as strings... am I being incredibly foolish to want this? There's probably an excellent reason for such functionality not to be desirable, and if that's the case, learning why would also be interesting.

    I hope that more or less made some sense to some of you...

  2. #2
    Student
    Join Date
    Apr 2001
    Location
    -
    Posts
    4,756
    perhaps something like
    PHP Code:
    car myClass:Class = ["level"+2];
    var 
    level:Level = new myClass(); 
    dont have flash around here but maybe thats a way to go

  3. #3
    Senior Member Awoogamuffin's Avatar
    Join Date
    Nov 2008
    Posts
    208
    Hey there renderhjs

    Thanks, but I'm afraid that doesn't work either - the problem is that square brackets are only used for arrays, so it thinks I'm trying to access an array. With parentheses it then thinks it's a string (so "Level1" as opposed to the class Level1).

    As far as I can tell, there isn't any way to do this with class names, or function calls for that matter.

    Like I said, it doesn't really matter - I'll just stick it in a switch statement somewhere hidden... probably as a static function for the Level class, as in

    var level:Level = Level.newLevel(1)

    and inside the Level class I'll have

    Code:
    public static function newLevel(inLevel:int):Level
    {
    
    var returnLevel:Level; switch(inLevel) {
    case 1: returnLevel = new Level1(); break; case 2: returnLevel = new Level2(); break; default: return null;
    } return returnLevel
    }
    It just seems a bit much, especially if I have like 30 levels... would be nice if I could treat function calls as strings is all...

  4. #4
    Senior Member
    Join Date
    Jan 2006
    Location
    USA
    Posts
    383
    I'm not sure instantiating a class dynamically is really the way to go with this.

    I'm assuming that all of your levels inherit from the Level class? If I were you, I'd create a format in XML to represent a level. This gives more options on how to change levels. You could parse all the levels at initialization and store them in an array, or parse a level during run-time. This also gives you more flexibility in creating levels, in my opinion.

    Just a thought.

  5. #5
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Code:
    var levelClass:Class = getDefinitionByName("Level"+levelIndex);
    var level:Level = new levelClass();
    But I agree with AfternoonDelite, it's probably much better to create a single class that can parse some external data to configure the level.

  6. #6
    Senior Member Awoogamuffin's Avatar
    Join Date
    Nov 2008
    Posts
    208
    Hey there

    thanks for the help guys!

    As for the XML external data idea, I'm definitely interested in that - not for this specific game (I need to code the levels in AS3 because of their heavy use of event listeners and level-unique interface elements) but for another game, I'm thinking of writing a level editor which will later save the level in some external format, the problem being which format to use. XML sounds fantastic, what is it? and where can I learn more?

    5TonsOfFlax - thanks for the getDefinitionByName thing. I'll give it a go tomorrow when I haven't come back from too many pints to code in any intelligible way...

  7. #7
    Pumpkin Carving 2008 ImprisonedPride's Avatar
    Join Date
    Apr 2006
    Location
    Grand Rapids MI
    Posts
    2,379
    Also, ClassFinder works the same way as getDefByname.
    The 'Boose':
    ASUS Sabertooth P67 TUF
    Intel Core i7-2600K Quad-Core Sandy Bridge 3.4GHz Overclocked to 4.2GHz
    8GB G.Skill Ripjaws 1600 DDR3
    ASUS ENGTX550 TI DC/DI/1GD5 GeForce GTX 550 Ti (Fermi) 1GB 1GDDR5 (Overclocked to 1.1GHz)
    New addition: OCZ Vertex 240GB SATA III SSD
    WEI Score: 7.6

  8. #8
    Member
    Join Date
    May 2008
    Location
    Ohio, USA
    Posts
    63
    Quote Originally Posted by Awoogamuffin
    As for the XML external data idea, I'm definitely interested in that - not for this specific game (I need to code the levels in AS3 because of their heavy use of event listeners and level-unique interface elements) but for another game, I'm thinking of writing a level editor which will later save the level in some external format, the problem being which format to use. XML sounds fantastic, what is it? and where can I learn more?
    XML stands for Extensible Markup Language. Depending on what kind of game you're making, XML can make life much easier for you. For example, say you have a tile-based game and need to load maps as you enter each area. You could use an XML file like this:

    <map>
    <tilerow>
    <tile>tile1</tile>
    <tile>tile2</tile>
    </tilerow>
    <tilerow>
    <tile>tile3</tile>
    <tile>tile4</tile>
    </tilerow>
    </map>

    That would represent a basic 2 x 2 grid of 4 tiles. The individual tiles are named tile1, tile2, tile3, and tile4. You would then write a function that would extract the XML data, arrange those tiles, and add them to the Stage. You would obviously use a much longer list for an actual map, but you get the idea. You could do the same thing with a two-dimensional array, but using an XML file is more hierarchical and lets you store multiple values per variable, so it can sometimes be more convenient.

    Take a look at these tutorials for more info:
    AS3 - http://www.8bitrocket.com/newsdispla...?newspage=6077
    AS3 - http://www.kirupa.com/developer/flas...ml_as3_pg1.htm
    AS2 - http://www.emanueleferonato.com/2007...le-based-game/
    Last edited by DoomOfTheLiving; 11-04-2008 at 01:28 AM.

  9. #9
    Custom User Title Incrue's Avatar
    Join Date
    Feb 2004
    Posts
    974
    Thanks, but I'm afraid that doesn't work either - the problem is that square brackets are only used for arrays,
    http://www.kirupa.com/forum/showthre...threadid=12082

  10. #10
    Student
    Join Date
    Apr 2001
    Location
    -
    Posts
    4,756
    that would make it then:
    PHP Code:
    var level:Level this["level"+2](); 
    or so I think

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center