A Flash Developer Resource Site

Results 1 to 12 of 12

Thread: [RESOLVED] Load last played level Shared Objects

  1. #1
    Designer, Programmer, Musician angelhdz's Avatar
    Join Date
    Mar 2010
    Posts
    971

    resolved [RESOLVED] Load last played level Shared Objects

    Hello again! My friend Nig 13 helped me with a Shared Object matter. I'm new to shared objects, so, thanks for helping.

    I'm creating a 100floors (android, iPhone) like game, and I need to save the last level played, so when you press "continue" it takes you to it.

    I have this dynamic text box called "rooms" and everytime you do the thing to go to the next level, the code is
    Actionscript Code:
    rooms++

    And in the "continue" button i have this:

    Actionscript Code:
    function loadlvl(){
        if(rooms==1){
        gotoAndStop("lvl1");
       
    }
    else if(rooms==2){
        gotoAndStop("lvl2");
       
    }
    else if(rooms==3){
        gotoAndStop("lvl3");
       
    }
    else if(rooms==4){
        gotoAndStop("lvl4");
       
    }
    else if(rooms==5){
        gotoAndStop("lvl5");
       
    }
    else if(rooms==6){
        gotoAndStop("lvl6");
       
    }
    else if(rooms==7){
        gotoAndStop("lvl7");
       
    }
    }

    loadBTN.onPress=function(){
       
     loadlvl();

    }

    And i have this in the first frame where is the Main screen with the New Game/ Continue options:

    Actionscript Code:
    var saveState:SharedObject = SharedObject.getLocal("cookie");

    if(saveState.data.rooms != undefined){


    } else {
      loadlvl();
      saveState.flush();
       
       
    }

    Do I have to specify something else on the buttons that jump to the next level?
    Actionscript Code:
    rooms++

    saveState.data.rooms++  //someething like this?
    ????

    What am I missing? Thanks

  2. #2
    Designer, Programmer, Musician angelhdz's Avatar
    Join Date
    Mar 2010
    Posts
    971
    OKAY! DONE!


    I changed rooms++ to rooms=1, rooms=2, rooms=3, etc. because if I use rooms++, if the last played level was level 3, and I refresh, when I press Continue button, it would be 3 +1 =4! Level 4! So instead of increasing the rooms var with rooms++ , I specify each level with room=1, room =2, and so on.

    Now, with the shared object i did this:

    In the Continue button:

    Actionscript Code:
    function loadlvl(){
        if(saveState.data.rooms==1){
        gotoAndStop("lvl1");
       
    }
    else if(saveState.data.rooms==2){
        gotoAndStop("lvl2");
       
    }
    else if(saveState.data.rooms==3){
        gotoAndStop("lvl3");
       
    }
    else if(saveState.data.rooms==4){
        gotoAndStop("lvl4");
       
    }
    else if(saveState.data.rooms==5){
        gotoAndStop("lvl5");
       
    }
    else if(saveState.data.rooms==6){
        gotoAndStop("lvl6");
       
    }
    else if(saveState.data.rooms==7){
        gotoAndStop("lvl7");
       
    }
    }

    loadBTN.onPress=function(){

    loadlvl();

    }


    And in the first frame of each level:

    Actionscript Code:
    room=1

    saveState.data.rooms=rooms;


    Thanks anyway! But, if you think there's a better way to achieve this, ideas welcome!

  3. #3
    Prid - Outing Nig 13's Avatar
    Join Date
    Jul 2006
    Location
    Norway
    Posts
    1,864
    Hi 8D,

    would you please post the game when you're done? I'd like to play it

    As for the level loading, why not just use a for loop ?

    Actionscript Code:
    total_lvls = 7;

    function loadlvl(){
        for(i=1;i<total_lvls+1;i++){
            if(saveState.data.rooms == i){
                gotoAndStop("lvl"+i);
            }
        }
    }
    I am back, guys ... and finally 18 :P

    BRING BACK THE OLD DESIGN!! OR AT LEAST FIX THE AS TAGS

  4. #4
    Designer, Programmer, Musician angelhdz's Avatar
    Join Date
    Mar 2010
    Posts
    971
    Of course you will play it! Hehe

    I'm not familiarized with the "for" loop...I'm scared of it hahaha because i don't undestand it. But i know when I learn how to use it, it will make my coding easier and faster. But i will start right now to read about it and how to implement it in my game.

    I'm developing level 8 right now hehe, the game will have 2 seasons of 50 levels each. 100 lvls total. Thanks, take care my friend

  5. #5
    Prid - Outing Nig 13's Avatar
    Join Date
    Jul 2006
    Location
    Norway
    Posts
    1,864
    Oh, sorry, since you're so good at actionscript, I thought you might know of it as well. It's just as you say it is, it looks really scary at first, but when you learn it, you laugh at yourself for being scared of it, at least that's how I felt when I familiarized myself with it

    for(starting variable value; condition; change per loop)

    The starting value is basically where you declare the variable itself (variable i), and its starting value, for instance, it will start with a value of 0. Then, in the condition, you specify what condition the variable should meet for it to run (badly explained, I know -.-"), for instance, the loop should run as long as your variable is less than 3 (once your variable is either equals to 3 or greater, the loop will stop, as the condition is no longer met, which makes it run). The last parameter, change per loop, is what change you want your variable to undergo. If it's idle, like, it stays as its starting value, for instance 0, with a condition of the variable being less than 3, then it would run infinitely, as the condition would always meet. What you want to do in the last parameter, is to either increment the variable or decrement it. This change will happen for every loop.

    Now, to explain the above better with an example. Let's say you just feel like making a for loop which traces the variable in the for loop, just to see how it works. Then, try this (spaces included for better overview, I don't like having spaces between equals sign and variable when writing for loops, because that's how I first saw it, lol):

    Actionscript Code:
    for(i = 0; i < 3; i++){
        trace(i);
    }

    It will trace, 0, 1 and 2. Why? Because first of all, the last parameter, it doesn't happen in the first loop. Speaking of loops, what happens in a for loop is basically that, until the condition is met, the same set of code is executed over and over again, and every time it is re-executed, the change per loop is executed. To explain that better as well ('cause my explaining is poor), the above code is the same as this:

    Actionscript Code:
    i = 0;

    trace(i);

    i++;

    trace(i);

    i++;

    trace(i);

    First of all, the variable which is going to be changed in the loop, is declared with its starting value, in this case, 0. Then, the actual code inside the for loop is executed. Then, the change happens, in this case, variable i is incremented by one, making its value now 1. Then, the same code inside the for loop is executed AGAIN, and the only difference is that variable i is now 1, instead of 0. After that, the change is undergone again, making variable i increment by one, making its value now 2. Then, the actual code is executed (yes, again). Then, the change occurs again, incrementing variable i by one, making it now 3, but every time the change happens (when the variable is incremented), the for loop then checks if the condition is still met. In this case, when variable i is changed to 3, the condition is no longer met, because variable i is NOT less than 3, it is in fact 3, which then results in the for loop stopping, so when variable i increments to 3, your code is NOT re-executed, only as long as the condition is met.

    What this variable i (or any other name, a, b, o, p, j, you can even use words, but it's most common to use a letter in for loops to keep them simple, and i is the most recurring variable name in terms of for loops) can be used for, you ask? Well, let's say you wanted to execute the same code 10 times, but with only a number changing. Then you'd simply use a for loop to save a lot of work and space (I'm sure you know of finding variables/objects dynamically, like join strings and other variables to look for the name they're making up, right?):

    Actionscript Code:
    for(i=1;i<11;i++){
        this["var_"+i] = i;
    }

    This would create variables, var_1, var_2, var_3, var_4, var_5, var_6, var_7, var_8, var_9, var_10, with their value the same as their number (var_1 = 1; var_3 = 3; var_10 = 10; ). You may have noticed the condition being, when i is less than 11, why? Because when it's less than 11, the value of i, in the last loop, will be 10. Alternatively, you could've used, i<=10, or, i==10, but it is advised to use less than or greater than in for loops, just in case, meaning that you can either use, i<11 or i<=10, but not recommended to use, i==10.

    Another good example would be to use for loops with Arrays:

    Actionscript Code:
    fruits = new Array("apple", "banana", "cherry");

    for(i = 0; i < fruits.length; i++){
        trace(fruits[i]);
    }

    As you may now, the Array index starts at 0, as in, the first item's index is 0, the second one's 1, the third one's 2, and so on... BUT, the LENGTH of an Array is NOT based on that, the length property (Array.length) returns the actual number of items in the Array, like, in this case, there are 3 items in the Array, then the length would be 3 as well, but still, the last item's index is 2, because Array index starts at 0, but the length starts at 1, like we human perceive it. Therefore, you can start the variable at 0, and in the condition, make it less than the Array's length. In this case, the array's length is 3, so the loop will run as long as variable i is less than 3 (array's length), making variable i's values through the loop, 0, 1 and 2, just what you need to access all the items in the Array. However, if you have a custom variable as the limit, and the starting value as 1, like this:

    Actionscript Code:
    end = 3;

    for(i=1;i<end;i++){
        trace(i); // 1, 2
    }

    Surely you would want 3 to be included in the loop? Then, you can either change the condition to, i<=end, OR, you can change it to, i<=end+1, if you get it:

    Actionscript Code:
    end = 3;

    for(i=1;i<end+1;i++){
        trace(i); // 1, 2, 3
    }

    -----------------

    Hope this is helpful and enhances your knowledge of for loops
    I am back, guys ... and finally 18 :P

    BRING BACK THE OLD DESIGN!! OR AT LEAST FIX THE AS TAGS

  6. #6
    Designer, Programmer, Musician angelhdz's Avatar
    Join Date
    Mar 2010
    Posts
    971
    Omg Nig! This will save me a lot of copy pasting , and swf file space! Haha!

    Now i began to understand the for loops. I put your for loop script in my game and it worked really great! Now im playing with the for loop in a new flash document to see all the possibilities. Thanks, i know I will be as good as you in a non far future hehe!

    Is like ALL THIS:

    Actionscript Code:
    if(saveState.data.rooms==1){
        gotoAndStop("lvl1");
       
    }
    else if(saveState.data.rooms==2){
        gotoAndStop("lvl2");
       
    }
    else if(saveState.data.rooms==3){
        gotoAndStop("lvl3");
       
    }
    else if(saveState.data.rooms==4){
        gotoAndStop("lvl4");
       
    }
    else if(saveState.data.rooms==5){
        gotoAndStop("lvl5");
       
    }
    else if(saveState.data.rooms==6){
        gotoAndStop("lvl6");
       
    }
    else if(saveState.data.rooms==7){
        gotoAndStop("lvl7");
       
    }

    Resumed into THIS:

    Actionscript Code:
    for(i=1;i<total_lvls+1;i++){
            if(saveState.data.rooms == i){
                gotoAndStop("lvl"+i);
            }
        }

    I love action script 2!!! Hehe!!!

    Do you work with AS3? I have seen the equivalent AS2 scripts in AS3 and it takes more scripting, to achieve a simple thing!!! I'm scared about that too! Hehe! See ya ^_^

  7. #7
    Prid - Outing Nig 13's Avatar
    Join Date
    Jul 2006
    Location
    Norway
    Posts
    1,864
    Haha, no problem, just wanted to give you a good guidance instead of trying to find tutorials xD

    Yeah, I work on AS3 from time to time, but only to help people with simple AS3 problems, but I learn a lot from though, and AS3 requires you to write a lot more, yes, which is why I hate it >.>

    Like, clicking on a button in AS2 is simple as this:

    Actionscript Code:
    btn.onPress = function(){
        trace("CLICKED A BUTTON, YAY :D");
    }

    but in AS3:

    Actionscript Code:
    import flash.events.MouseEvent;

    btn.addEventListener(MouseEvent.MOUSE_DOWN, btnClick);

    function btnClick(e:MouseEvent){
        trace("CLICKED A BUTTON.... -.-*");
    }

    If you've looked into Java (I have, some video tutorials on YouTube), then you'd notice that AS3 is more true to OOP (object-oriented programming) than AS2, it follows the same structure as the great programming languages, like Java, which can be difficult for people like me, who loves AS2 (and you)... Basically, events are created in a seperate .as Class file, located in /...path to Flash.../flash/events/MouseEvent.as, and then you import that file to Flash, so its functions and properties can be used (for instance, MouseEvent.MOUSE_DOWN or MouseEvent.MOUSE_UP, these are codes inside MouseEvent.as). These are events, something which occurs, like when you press your Mouse down, or up after pressing it down (release), and in AS3, you have to LISTEN for these events, like execute codes when these events occur. To add a listener like this, you use addEventListener, and then you make a function which will be executed when that event occurs. In the function, though, you have to specify a parameter, which is used to access a lot of handy stuff, for example the target, the one who's listening for the event, in this case, btn, you can retrieve its name through this one parameter in the function. Another thing in AS3, is that you HAVE to specify datatypes, like, var someString:String = "string; you cannot overlook it like in AS2, string = "string"; and I hate this as well...... There are a lot more differences, but I am not intending to move to AS3, I'm staying with AS2 (unless they remove that function when AS4 comes, but then I'll rage and protest and make them bring it back, lol)
    I am back, guys ... and finally 18 :P

    BRING BACK THE OLD DESIGN!! OR AT LEAST FIX THE AS TAGS

  8. #8
    Designer, Programmer, Musician angelhdz's Avatar
    Join Date
    Mar 2010
    Posts
    971
    :'( AS2...R.I.P. we all remember you! Hahaha!

    I don't want to think about as2's death...but anyway! Let's enjoy it while it lasts!

    One last thing...this game, i don't want to overcharge the SWF with all the levels (it would be like a MONSTER SWF!! lol ) If i take apart each level, and put them in a separate SWF and load them all with loadMovie into empty movieclips in a MAIN SWF, how do I pass the variables?

  9. #9
    Prid - Outing Nig 13's Avatar
    Join Date
    Jul 2006
    Location
    Norway
    Posts
    1,864
    Use MovieClipLoader class instead, as it ships with an onComplete event handler, which basically lets you execute code when the external SWF has been completely loaded into your Flash movie and its first frame has been executed:

    Actionscript Code:
    mcLoader = new MovieClipLoader();
    mcLoader.loadClip("external.swf", mc_container);

    loading = new Object();
    loading.onLoadInit = function(){
        passed_var = mc_container.external_var;
    }
    mcLoader.addListener(loading);

    Hope this makes sense, basically, your external SWF is inside a movieclip, and you can access its variables by referring to that movieclip and then the variable
    I am back, guys ... and finally 18 :P

    BRING BACK THE OLD DESIGN!! OR AT LEAST FIX THE AS TAGS

  10. #10
    Designer, Programmer, Musician angelhdz's Avatar
    Join Date
    Mar 2010
    Posts
    971
    mc_container would be the empty movieclip that i would create in the scene, to load the external swf, right?

    And this is the panoram. Each level has buttons. One of them will go to the next level on press.
    Actionscript Code:
    gotoAndStop("level2");

    I have the main SWF with empty movieclip on stage, on each frame acording to the level, and I load the external levels SWF's into these movieclips, but when I say "gotoAndStop("level2");" will it go to level 2 of which timeline? Main swf timeline? loaded level swf timeline?

    And the saveState.data.rooms script, goes in the Main SWF?

    Oh God! This is getting so hard!
    Last edited by angelhdz; 06-24-2012 at 10:01 PM.

  11. #11
    Prid - Outing Nig 13's Avatar
    Join Date
    Jul 2006
    Location
    Norway
    Posts
    1,864
    Timeline of the loaded SWF, but you know, when that movie is loaded into mc_container, it becomes a part of your Main Flash, so you can refer to your Main Timeline using _root.

    In your external swf, use:

    Actionscript Code:
    _root.gotoAndStop("level2");
    I am back, guys ... and finally 18 :P

    BRING BACK THE OLD DESIGN!! OR AT LEAST FIX THE AS TAGS

  12. #12
    Designer, Programmer, Musician angelhdz's Avatar
    Join Date
    Mar 2010
    Posts
    971
    Ok, i will test it, and I'll tell hehe

    Althought, the swf size is still 3Megabytes, 7 levels. I will keep making 2 .FLA's, one with all the levels, and the EXPERIMENT haha, the main swf loading the child swf's to compare sizes. Thanks again!

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