Skip navigation
PaulQmmQ
Currently Being Moderated

How to manage select levels in flash games ?

Nov 4, 2011 9:46 PM

Tags: #action_script_3 #game #actionscript3 #game_programming

I was wondering what's the best practice to make something like below, I thought on global variables with if conditionals, but the code is going to be messy. Unfortunately I can't find any tutorial on the internet, I spent hours googling it but everything I found there was nothing to do with this. It's so frustrating because it's really hard to get on without these "special" references.

 

For instance,

 

After finishing a level:

http://i.imgur.com/44iMa.png

 

 

 

Select level screen:

http://i.imgur.com/bKCYc.png

 

Game: Rubble Trouble Tokyo

 

 

Thanks in advance.

 
Replies
  • Peter Celuch
    505 posts
    Nov 17, 2005
    Currently Being Moderated
    Nov 5, 2011 4:10 AM   in reply to PaulQmmQ

    If you have your buttons placed on the stage, you can use their name to extract level ID. To cycle through buttons when initializing, you should put them in a holder (MovieClip) and give it a name let's say "levelButtonsHolder".

    Name your buttons like this: btn_%LEVEL-ID%

    for eaxample: btn_level0, btn_level1, ..., btn_levelN, where "levelX" is ID for a level by which you can instantiate and reference the level.

     

    Then your code should look like:

     

    import flash.events.MouseEvent;
     
    for(var i:int = 0; i < levelButtonsHolder.numChildren; i++) {
         var button:MovieClip = levelButtonsHolder.getChildAt(i) as MovieClip;
         if(button.name.indexOf("btn_") == -1) continue;
         button.addEventListener(MouseEvent.CLICK, levelButton_clickHandler);
    }
     
    function levelButton_clickHandler(event:MouseEvent):void {
         var levelID:String = MovieClip(event.currentTarget).name.substr(4);
         trace("LOAD LEVEL. Level ID: " + levelID);
    }
    
     
    |
    Mark as:
  • Peter Celuch
    505 posts
    Nov 17, 2005
    Currently Being Moderated
    Nov 5, 2011 4:17 AM   in reply to PaulQmmQ

    If your levels are MovieClips with classes attached to them, you can instantiate them using acquired LevelID. LevelID should be unique part of a level's full qualified class name.

     

    Let's say your levels have classes:

     

    game.levels.Level01
    game.levels.Level02
    ...
    

     

    In that case you should give your buttons names:

     

    btn_Level01
    btn_Level02
    ...
    

     

    and you should modify the levelButton_clickHandler() function:

     

    import flash.system.ApplicationDomain;
     
    function levelButton_clickHandler(event:MouseEvent):void {
        var levelID:String = MovieClip(event.currentTarget).name.substr(4);
        var levelClass:Class = ApplicationDomain.currentDomain.getDefinition("game.levels." + levelID) as Class;
     
        var level:MovieClip = new levelClass();
    }
    
     
    |
    Mark as:
  • Peter Celuch
    505 posts
    Nov 17, 2005
    Currently Being Moderated
    Nov 7, 2011 3:36 PM   in reply to PaulQmmQ
    if(button.name.indexOf("btn_") == -1) continue;
    

     

    What is it checking out ?

     

         This is just bulletproofing - if, by any mistake you have some piece of graphics, background, whatever in the buttons holder, it sure won't be named "btn_..." and we will not consider it button - continue means "go to next iteration, don't execute further for this item"

     

     

     

    public class Level01 extends Sprite
    {
     
    public static var allowToPlay = false;
     
    public function Level01():void
    {
    }
    }
    

     

     

    for(var i:int = 0; i < levelButtonsHolder.numChildren; i++) {
         var button:MovieClip = levelButtonsHolder.getChildAt(i) as MovieClip;
         if(button.name.indexOf("btn_") == -1) continue;
         if(button.name.substr(4).allowToPlay == false) continue;
         button.addEventListener(MouseEvent.CLICK, levelButton_clickHandler);
    }
    

     

     

    I wouldn't use static variables. Maybe you could pull that off, but this syntax wouldn't definitely work.

    if(button.name.substr(4).allowToPlay == false) continue;

     

    What I would do is create some array with respect to what level user can play. And than compare it against button names.. forexample

     

    var myUserPermissions:Array = [
         {
              levelID: "Level01",
              allowToPlay: true
         },
         {
              levelID: "Level02",
              allowToPlay: true
         },
         {
              levelID: "Level03",
              allowToPlay: false
         }
    ];
    

    and then check each button:

     


    for(var i:int = 0; i < levelButtonsHolder.numChildren; i++) {
         var button:MovieClip = levelButtonsHolder.getChildAt(i) as MovieClip;
         if(button.name.indexOf("btn_") == -1) continue;
         var levelID:String = MovieClip(event.currentTarget).name.substr(4);
         if(!canPlayLevel(levelID)) continue;
         button.addEventListener(MouseEvent.CLICK, levelButton_clickHandler);
    }
     
    function canPlayLevel(levelID:String):Boolean {
         for(var i:int = 0; i < myUserPermissions.length; i++) {
              if(myUserPermissions[i].levelID == levelID) {
                   return myUserPermissions[i].allowToPlay;
              }
         }
         return false;
    }
    

     
    |
    Mark as:
  • Peter Celuch
    505 posts
    Nov 17, 2005
    Currently Being Moderated
    Nov 9, 2011 9:56 AM   in reply to PaulQmmQ

    Glad to hear it. Good luck!

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points