Really BIG Problem Level Select AS3

Subscribe to Really BIG Problem Level Select AS3 33 posts

Sign in to reply


 
avatar for Gamgam0 Gamgam0 98 posts
Flag Post

Hey I was making a level select page for my game when I realized I had no clue how to. I have a variable called “levelscomplete” which stores the numeric value of the highest level completed. I want to make a level select page to go to an unlocked level. If the level is locked then I want the text to change to locked. Does anybody have any idea or know of any tutorials to help.

Thanks in advance.

 
avatar for KMAE KMAE 439 posts
Flag Post

I’m going to run into this problem soon myself, but haven’t made it to that point yet. Good luck.

 
avatar for Gamgam0 Gamgam0 98 posts
Flag Post

thanks anybody else have any ideas

 
avatar for L3viath3nt L3viath3nt 166 posts
Flag Post

If I were you, I would add the buttons for the level select page dynamically, store necessary information in an array, and then you could have each button go to whichever frame it needed to based on the information stored about it.

 
avatar for MaToMaStEr MaToMaStEr 628 posts
Flag Post

Like l3viath3nt said…. using your levelsComplete variable you should dynamically add the buttons.
But only the buttons until levelsComplete+1 should catch the onRelease method

 
avatar for Gamgam0 Gamgam0 98 posts
Flag Post

How would you do that?

 
avatar for MaToMaStEr MaToMaStEr 628 posts
Flag Post

There are really several ways of doing this…
For example, if your buttons are already on Stage… you could write something like this on the menu Frame. Im supposing your buttons have instance names of [’button’+n] (with n from 1 to totalLevels).

Since I don’t know AS3, i’ll post what i might do in AS2

for(n=1;n<=totalButtons;n++){
    _root["button"+n].num = n;
    _root["button"+n].onRelease = function(){
        if(this.num <= (levelsComplete+1)) _root.gotoAndStop("level"+this.num);
    }
    if(n>(levelsComplete+1)) _root["button"+n].gotoAndStop("lockedFrame");
}

First you assign to the ‘num’ variable within the button, the number of the level it corresponds. Then you make it so when you click on the button, it jumps to that level (assuming it has a label of ’level’+n). The last line just changes the apearnce of the button in the case it’s locked.

 
avatar for rarapompoms rarapompoms 184 posts
Flag Post

I read the OP and just knew the first answer would be frames. Please don’t listen to them. On your level select screen you’ll have something to place your buttons. I’m going to guess it’s a loop, so you’ll want something like this maybe:

for(var i:int = 0; i < numLevels; i++){

    if(i <= levelsComplete)
        placeUnlockedButton(i);
    else
        placeLockedButton(i);

}

You’re placeUnlockedButton(num:int) function should just create a new button, give it the right lable and place it in the right position. The placeLockedButton(num:int) does the same but makes the button unclickable and has puts “locked” over it.

 
avatar for Gamgam0 Gamgam0 98 posts
Flag Post

Huh? Each of my level buttons are dynamic text boxes how do I make them loops?

Sorry I’m a beginner

 
avatar for MaToMaStEr MaToMaStEr 628 posts
Flag Post

What i wrote should work, as long as you give each of your buttons the appropriate instance names.

 
avatar for Gamgam0 Gamgam0 98 posts
Flag Post

That’s as2 how do I do in as3

 
avatar for ldstrumpet ldstrumpet 141 posts
Flag Post

That is not AS2 it is AS3, i even used it to make my own level select thing. At least i made it AS3 it is not very hard all you need are a couple new brackets. My final usage of that code is just:


for(var i:int = 1; i <= numLevels; i++){
if (i <= levelsComplete) {
placeUnlockedButton(i);
}
else {
placeLockedButton(i);
}
}

 
avatar for Gamgam0 Gamgam0 98 posts
Flag Post

okay i get that but what do I put in my placeUnlockedbutton(i) and my placeLockedButton(i);

 
avatar for Gamgam0 Gamgam0 98 posts
Flag Post

please does anybody have any ideas i dont know what to put in my placeLockedButton and placeUnlockedButton

 
avatar for SuperMarioJump SuperMarioJump 303 posts
Flag Post

Make a new button, give it the right label (probably "Level " + i), and place it in the right place. You might want to make a LevelButton class for this so you can do something like levelButton.locked = true; or you might find it easier to just use SimpleButton/Sprite/whatever. Your choice.

 
avatar for Gamgam0 Gamgam0 98 posts
Flag Post

i have buttons with instance names of ls1 (level select 1) and ls2 and so on

What am I doing wrong?

 
avatar for SuperMarioJump SuperMarioJump 303 posts
Flag Post

You’ve already placed these on the stage in Flash? Well do something like:

for(var i:int = 1; i < numLevels; i++){

     var b:TextField = getChildByName("ls" + i);

     if(i <= levelsComplete){
          b.text = String(i);
     }else if(i > levelsComplete){
          b.text = "Locked";
     }

}

You’ll need something that will stop the locked levels being clicked on, but that depends on how you are detecting the mouse clicks.

 
avatar for Gamgam0 Gamgam0 98 posts
Flag Post

K but two things I am dectecting clicks my mouse event click also when an unlocked level is clicked I need it to do 2 things make a variable called retry true and gotoandstop a frame. How would I do that?

 
avatar for Gamgam0 Gamgam0 98 posts
Flag Post

Please does anyone know how to fix my problem?

 
avatar for rarapompoms rarapompoms 184 posts
Flag Post

We’re probably too far gone to stop you using frames:

for(var i:int = 1; i < numLevels; i++){

     var b:TextField = getChildByName("ls" + i);
     b.addEventListener(MouseEvent.MOUSE_DOWN, onButtonClicked);

     if(i <= levelsComplete){
          b.text = String(i);
     }else if(i > levelsComplete){
          b.text = "Locked";
     }

}

private function onButtonClicked(e:MouseEvent):void{

     if(TextField(e.currentTarget).text == "Locked"){
          return;
     }else{
          retry = true;
          gotoAndStop(int(TextField(e.currentTarget).text));
     }

}

I’ve no idea if that’ll work but it should give you some idea. I’d consider writing a custom event for this but it might not be necessary. There are plenty of more elegant ways to do a level select but as I’m sure you’re not willing to do a complete rewrite we’re using the traditional Flash method of just piling stuff on top of what we already have until it somewhat works. I don’t know anything about frames because I consider myself a good person, but the for loop needs to run once every time the level select screen is opened, and the function obviously needs to be in it’s scope. You’ll also need a constant called numLevels (or TOTAL_LEVELS if you’re into that) and variables called retry and levelsComplete. If your levels aren’t on frames 1, 2, 3 etc you’ll have to modify the gotoAndStop like this:

gotoAndStop(int(TextField(e.currentTarget).text) + 2);

or whatever.

 
avatar for Gamgam0 Gamgam0 98 posts
Flag Post

I tried that but I got some errors. First I got an error at private function. I changed it to function and it worked. Second on the var b:TextField = getChildByName(“ls” + i);
I get the error:

1118: Implicit coercion of a value with static type
flash.display:DisplayObject to a possibly unrelated type flash.text:TextField.

I changed the code a little bit here is what is is


 // sf.data.levelscomplete is the number of levels competed (its a save file)

for(var i:int = 1; i < 28; i++){

     var b:TextField = getChildByName("ls" + i);
     b.addEventListener(MouseEvent.MOUSE_DOWN, onButtonClicked);

     if(i <= sf.data.levelscomplete){
          b.text = String("Level" + i);
     }else if(i > sf.data.levelscomplete){
          b.text = "Locked";
     }

}

function onButtonClicked(e:MouseEvent):void{

     if(TextField(e.currentTarget).text == "Locked"){
          return;
     }else{
          retry = true;
          gotoAndStop(int(TextField(e.currentTarget).text));
     }

}

 
avatar for Gamgam0 Gamgam0 98 posts
Flag Post

anybody have any idea?

 
avatar for I_wear_shoes I_wear_shoes 44 posts
Flag Post

Try changing it to:

var b:TextField = TextField(getChildByName("ls" + i));
 
avatar for Gamgam0 Gamgam0 98 posts
Flag Post

K my buttons are dynamic text buttons do I have the instance name ls1 or do I have the text actually be ls1

 
avatar for SuperMarioJump SuperMarioJump 303 posts
Flag Post

The instance names should be ls1, ls2 etc.

Sign in to reply