6 Replies Latest reply: Nov 9, 2011 9:56 AM by Peter Celuch RSS

    How to manage select levels in flash games ?

    PaulQmmQ

      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.

        • 1. Re: How to manage select levels in flash games ?
          Peter Celuch Community Member

          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);
          }
          
          • 2. Re: How to manage select levels in flash games ?
            Peter Celuch Community Member

            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();
            }
            
            • 3. Re: How to manage select levels in flash games ?
              PaulQmmQ Community Member
              if(button.name.indexOf("btn_") == -1) continue;

               

              What is it checking out ?

               

              I also need to check out whether the level is available for the user to play, if so then to place the button on the stage. Because the user can't play Level 2 if he/she didn't win the Level 1.

               

              Looking at you code carefully and learning from them, can I do that, will it work ?

               

               

               

              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);
              }
              

               

               

              Thanks so much.

              • 4. Re: How to manage select levels in flash games ?
                Peter Celuch Community Member
                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;
                }
                

                • 5. Re: How to manage select levels in flash games ?
                  PaulQmmQ Community Member

                  Thanks so much, Peter. You helped and taught me that I've been trying to understand what was the best way to accomplish this.

                  • 6. Re: How to manage select levels in flash games ?
                    Peter Celuch Community Member

                    Glad to hear it. Good luck!