dcsimg
A Flash Developer Resource Site

Page 1 of 2 12 LastLast
Results 1 to 20 of 25

Thread: Save Game

  1. #1
    Senior Member
    Join Date
    Jul 2004
    Posts
    140

    Save Game

    If you are creating a game and you want your users to be able to save their current location, level, points, settings, etc then using this script is what you need.
    If you understand SharedObject.getLocal then this will be easy.
    Say for example you have a Dynamic Text box for the players score.
    The text box is called score1 and the text box variable is called score. As the player collects a certin item (kills an enemy, etc) his score goes up in the score1.text box. After awhile he may want to quit playing but return later and start off where he last left. So create a button. Next to the button put Save Game. Inside the buttons actionScript you need to add

    code:
    on (release) {
    //this takes the current score and places it in saveScore
    saveScore(score1.text);
    }



    this takes the data from score1.text.
    in order for it to actully save and recall the data you need to add to
    the keyframe that the save button is placed in and add in actions

    code:

    //reads any saved data from "test"
    //if "test" is changed the data is saved in a different location
    localInfo = SharedObject.getLocal("test");
    function saveScore() {
    localInfo.data.score
    }



    You now have the players score saved onto their hard drive.

    Now if you want the player to be able to recall the data...
    Add a load game button to your main page.
    In the laod game button add
    code:

    gotoAndStop("Scene 2", 1);//or whatever you have the game starting at
    _root.loadedgame = 1;



    the new game button should just have
    code:

    gotoAndStop("Scene 2", 1);//or whatever you have the game starting at



    In Scene 2 frame 1 you should have the following...

    code:

    if (_root.loadedgame == 1) {
    localInfo = SharedObject.getLocal("test");
    if (localInfo.data.score == undefined) {
    play();
    //makes the score 0 as if starting a new game
    //with no saved data found
    score = 0;
    } else {
    //makes score the saved data
    score = localInfo.data.score;
    } else if (!_root.loadedgame == 1) {
    //starts a new game, score is 0
    score = 0;
    }



    If you wanted to add more like the current level the player was on all you would need is to have a variable that says the current level, then when the player saves add
    saveLevel(level1.text);//make sure the text is called level1

    or you could have

    code:

    localInfo.data.level = 1//or current level, number is changed
    //in each different level
    then in the same keyframe with the save game button
    add

    function saveLevel() {
    localInfo.data.level
    }
    //function saveScore() should also be here



    then when recalling the data

    code:

    if (_root.loadedgame == 1) {
    localInfo = SharedObject.getLocal("test");
    if (localInfo.data.score == undefined || localInfo.data.level == undefined) {
    play();
    //makes the score 0 as if starting a new game
    //with no saved data found
    score = 0;
    } else {
    //makes score the saved data
    level = localInfo.data.level
    if (level == 1) {
    gotoAndPlay("Scene 2", 1);
    } else if (level == 2) {
    gotoAndStop("Scene 3", 1);
    } //and so on
    score = localInfo.data.score;
    } else if (!_root.loadedgame == 1) {
    //starts a new game, score is 0
    score = 0;
    }



    its not to hard to change or add more/ less save data from there...
    if you don't understand somthing
    or need help, reply to this post...
    ill be making a fla for download so everyone can see what I mean if you dont understand. It may take a little to understand, but once you get it you can have many save game options.
    Last edited by crowebird; 07-28-2004 at 12:28 AM.

  2. #2
    doItLikeThis
    Join Date
    Jan 2004
    Location
    :noitacoL
    Posts
    1,080
    Good one mate, that will help people a lot, specially the noobs who wanna make there games savable.
    -Aditya

  3. #3
    Senior Member
    Join Date
    Jul 2004
    Posts
    107
    I uh, don't get it. So your creating a variable to save the game progress? What if I closed the window, and ran the game again. How would I load my game then?

  4. #4
    doItLikeThis
    Join Date
    Jan 2004
    Location
    :noitacoL
    Posts
    1,080
    To get more idea on shared ojects in flash mx, go here :-
    http://www.kirupa.com/developer/mx/sharedobjects.htm
    -Aditya

  5. #5
    Senior Member
    Join Date
    Jul 2004
    Posts
    107
    All you had to do was say it's like placing a cookie, only in flash. I would have understood that

  6. #6
    Senior Member
    Join Date
    Jul 2004
    Posts
    140
    yes it is like a cookie... it saves the data onto the users hard drive, and yes if you close the game and reload it the data will still be there as long as you call it up

  7. #7
    Senior Member
    Join Date
    Jul 2004
    Posts
    140
    and like I said... to recall data
    then when recalling the data

    code:

    if (_root.loadedgame == 1) {
    localInfo = SharedObject.getLocal("test");

    //this checks to see if anydata was saved in the first place
    if (localInfo.data.score == undefined || localInfo.data.level == undefined) {
    //if not...
    play();
    //makes the score 0 as if starting a new game
    //with no saved data found
    score = 0;
    } else {
    //data was saved, recalling and using saved data
    //as if the level was saved, recalls the data and sends the user
    level = localInfo.data.level
    if (level == 1) {
    gotoAndPlay("Scene 2", 1);
    } else if (level == 2) {
    gotoAndStop("Scene 3", 1);
    } //and so on
    //makes score the saved data
    score = localInfo.data.score;
    } else if (!_root.loadedgame == 1) {
    //starts a new game, score is 0
    score = 0;
    }




    Im building a guide for alot of flash things at my website...
    www.wtflash.com
    Last edited by crowebird; 07-28-2004 at 12:29 AM.

  8. #8
    Ihoss
    Guest
    One of the places its useful is in a game where you login. There you can store the username from last time in a cookie and then the user will have his name there when he opens the game again.

  9. #9
    Senior Member
    Join Date
    Jul 2004
    Posts
    140
    yes it is and here is an example of how you could load up the data, if you had other variables to save data

    code:

    this.createTextField("dData", myDepth, 50, 190, 150, 70);
    name.selectable = false;
    dData.selectable = false;
    dData.setNewTextFormat(Arial);
    dData.text = localInfo.data.user+","+newline+"Last time you played"+newline+"you won "+localInfo.data.winS+" round(s) and lost "+localInfo.data.losses+"."+newline+newline+"S tart New Game >>>";
    name = "Is '"+localInfo.data.user+"' not your name?"
    name2 = "Sign in as someone else >>>"



    EDIT:
    --sorry about the coding extending off the screen.
    Also... name and name2 were text box created on the screen not added with coding and next to name 2 was a button that inside stated the localInfo.data.name = undefined so a new name could be created.

    Im still working on a save game fla for download, I had created one eariler but it is sloppy and probably would be hard to understand... Im also adding this to my UFG [ultimate flash guide] at my site www.wtflash.com when I get the time
    Last edited by crowebird; 07-28-2004 at 12:29 AM.

  10. #10
    Hype over content... Squize's Avatar
    Join Date
    Apr 2001
    Location
    Lost forever in a happy crowd...
    Posts
    5,928
    Nice work crowebird, but any chance you could re-edit your posts using the [ as ] tag to make it clearer to read ?

    Squize.

  11. #11
    FK's Resident ...... Resident flashMasterHand's Avatar
    Join Date
    Aug 2003
    Location
    Connecticut, USA
    Posts
    168
    Nice tute, man. Should be a real helper for the newbies.

    I think it should be added to the Knowledgebase.

    Dave
    Programmers don't die, they just GOSUB without RETURN.

  12. #12
    Senior Member webgeek's Avatar
    Join Date
    Sep 2000
    Posts
    1,356
    Nice post!

    Another related trick is to serialize the state of your game objects using XML, WDDX or any other object serializer and save that string using SharedObjects. That way you can save and load complex objects very easily with just a single call. Have fun!

  13. #13
    Senior Member
    Join Date
    Jul 2004
    Posts
    140
    Ok, well I added the [ as ][ /as ] to make the script easier to read

  14. #14
    Junior Member
    Join Date
    Jul 2004
    Location
    Australia
    Posts
    4
    I've put all the actionscript in, it says there are no syntax errors, everything works fine except it either won't save or load. See the swf attachment. I've used this actionscript:

    New Game Button:

    code:
    on(release){
    gotoAndStop("Scene 2", 1);
    //or whatever you have the game starting at
    }



    Load Game Button:

    code:
    on(release){
    gotoAndStop("Scene 2", 1);
    //or whatever you have the game starting at
    _root.loadedgame = 1;
    }



    Save Game Button:

    code:
    on (release) {
    //this takes the current score and places it in saveScore
    saveScore(score.text);
    }



    Scene 2, Frame 1 (The one with the Fire! button on it)

    code:
    //reads any saved data from "test"
    //if "test" is changed the data is saved in a different location
    localInfo = SharedObject.getLocal ("test");
    function saveScore () {
    localInfo.data.score;
    }
    if (_root.loadedgame == 1) {
    localInfo = SharedObject.getLocal ("test");
    if (localInfo.data.score == undefined) {
    //makes the score 0 as if starting a new game
    //with no saved data found
    score = 0;
    } else {
    //makes score the saved data
    score = localInfo.data.score;
    }
    } else if (!_root.loadedgame == 1) {
    //starts a new game, score is 0
    score = 0;
    }



    Is there something I have done wrong?
    The Master of Flash MX...

  15. #15
    Senior Member
    Join Date
    Jul 2004
    Posts
    140
    here, let me make you a save data fla, using only MX...

    edit: btw, you didnt add an attachment
    ...the thing I can think you may of done wrong is that you didnt give the score text field the -instance name- of "score" you gave it the -variable name- of "score", so it couldnt find the text field, so it wouldnt save the data.
    Last edited by crowebird; 10-17-2004 at 09:17 PM.

  16. #16
    Senior Member
    Join Date
    Jul 2004
    Posts
    140
    the fla...
    Attached Files Attached Files

  17. #17
    Junior Member
    Join Date
    Jul 2004
    Location
    Australia
    Posts
    4
    Thanks, it's a relief to find a save game thing that actually works!
    The Master of Flash MX...

  18. #18
    Mя.ŋdŗ&#511 --eXe-eXtreme--'s Avatar
    Join Date
    Feb 2005
    Location
    Windsor On.
    Posts
    121

    Problem

    Every time I call the saved game data it is always 1 no matter what even when it is sapposed to be number 0.

    here is my fla..
    www.tno-clan.org/andersonprojects/database.fla
    I Use Flash 8
    Help is my last name, Freeload is my first :P
    إŦ№إ Mя. ŋdŗǿņ

  19. #19
    Junior Member
    Join Date
    Jul 2007
    Posts
    4
    trying to use this code for a save-feature on a leveling calculator i'm developing for players of an existing MMORPG... having some issues understanding where "test" came from and how it's implimented... downloaded the FLA and haven't had nearly enough time to look through it... but tested the resulting SWF and it worked like I want it to... as long as this can be used to save user-input then I'm golden...

    good job!

  20. #20
    Junior Member
    Join Date
    Jul 2007
    Posts
    4
    got it to work... the posted info isn't complete, but the FLA was easy to ingest... thanks!!! my calculator now has save functionality!

    one question though, where is this being stored, and what's the file being called? Test?

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