• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

How to manage select levels in flash games ?

Community Beginner ,
Nov 04, 2011 Nov 04, 2011

Copy link to clipboard

Copied

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.

TOPICS
ActionScript

Views

1.0K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Enthusiast , Nov 05, 2011 Nov 05, 2011

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 < le

...

Votes

Translate

Translate
Enthusiast ,
Nov 05, 2011 Nov 05, 2011

Copy link to clipboard

Copied

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

}

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Nov 07, 2011 Nov 07, 2011

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Nov 07, 2011 Nov 07, 2011

Copy link to clipboard

Copied

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.levelID == levelID) {

               return myUserPermissions.allowToPlay;

          }

     }

     return false;

}

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Nov 05, 2011 Nov 05, 2011

Copy link to clipboard

Copied

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

}

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Nov 09, 2011 Nov 09, 2011

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Nov 09, 2011 Nov 09, 2011

Copy link to clipboard

Copied

LATEST

Glad to hear it. Good luck!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines