Go Back   kirupaForum > Flash > Flash 8 (and earlier)

Reply
 
Thread Tools Display Modes
Old 11-09-2009, 08:37 PM   #1
Crystal
Registered User
Location San Diego, CA

Posts 36
Local Shared Object - Help!

Hello All-

I'm hoping someone could please assist me with this, or at least point me in the right direction... Kinda stuck here.

Anyway, I have created a very large Flash file (800 kf's) where I need to save user progress, variables, etc. I basically want the user to be able to hit the "save" button, which will save their current location. When they return, I would like them to be directly brought back to the keyframe they were last at, with all other variables saved as well.

I got this to work by creating the shared object in frame 1 and setting variables to determine whether or not they have completed certain sections. Essentially, (section1_completed ="no"; section2_completed = "no;" and so on...) in the first frame, and once they reach these sections I set those variables to "yes." In the first frame, I have written a huge conditional statement to determine whether or not these sections have been completed:

if(section1_completed == "yes"){
gotoAndStop("section1");
}

and so on....

Anyway, this seems to work just fine until I get to a certain point, then for some odd reason, it seems to break and no longer work. I'm not quite sure what's going on here.... Is there possibly a better way to save user location for such a large Flash file?? I just want to bring the user back to their current keyframe location.

Any suggestions are greatly appreciated! Thanks much!
Crystal is offline   Reply With Quote

Sponsored Links (Guests Only) - Register | Need Help?
 

Old 11-09-2009, 09:18 PM   #2
glosrfc
Registered User
 
glosrfc's Avatar
Location Halley Research Station, Latitude 75°35' S, Longitude 26°39' W, Brunt Ice Shelf, Coats Land, Antarctica

Posts 4,158
There are a number of options. Not sure why your if statement doesn't work* but it might be simpler to use a switch/case statement instead:
ActionScript Code:
switch(section_completed) {
  case 1:
    gotoAndStop("section1");
    break;
  case 2:
    gotoAndStop("section2");
    break;
}

If it's just a frame number that you want to go to you can also store the _currentframe in your Shared Object.

Finally, you can simply store the level number as follows:
ActionScript Code:
var section_completed:Number = 2; // this is the variable you save in your Shared Object to indicate progress through the game
var target_label:String = "section" + section_completed;
gotoAndStop(target_label);


*It might be failing to work because you're exceeding the default size for a Shared Object of 100k. However you should get a dialog window in the Flash Player asking if you want to exceed that size, in which case, it would then work.

__________________
©2006 GlosRFC - Searching 8,168,684,336 brain cells
glosrfc is offline   Reply With Quote
Old 11-09-2009, 10:15 PM   #3
Crystal
Registered User
Location San Diego, CA

Posts 36
Thank you so much for your response - this is VERY helpful! I always seem to make things more complex....

Yes, it was quite strange....the if statement was working just fine until I got further along in coding. There weren't any errors either, so I'm not sure what caused the glitch. I also set the Flash Player settings to "unlimited," so it seems that exceeding the 100k limit isn't the problem either.

Your suggested options should definitely work here. So, if I were to use the _currentframe option you suggested, I would simply create the shared object:

//create object
local_data = SharedObject.getLocal("user_location");
_currentframe = local_data.data._currentframe;

//then save the object with a save button
_root.saveBtn.onPress = function(){

// set data for the shared object
local_data.data._currentframe = _currentframe;

// write the data immediately
local_data.flush();
}

//then in frame one??

gotoAndStop(_currentframe);

Is this correct, or am I missing something here?? Thanks again! This has been extremely helpful.... Almost there!
Crystal is offline   Reply With Quote
Old 11-09-2009, 10:48 PM   #4
glosrfc
Registered User
 
glosrfc's Avatar
Location Halley Research Station, Latitude 75°35' S, Longitude 26°39' W, Brunt Ice Shelf, Coats Land, Antarctica

Posts 4,158
the_variable_to_save = _root._currentframe;
I also wouldn't use _currentframe as a variable name as it's likely to clash with the read-only Movieclip property by the same name. For example, gotoAndStop(_currentframe); is probably going to try to move the playhead to the current frame (i.e the frame it's currently on), rather than the one stored in your variable. If you stick to meaningful variable names such as "level" or "current_level", you won't go far wrong.

However using this _currentframe approach might not be suitable as it can take you to a frame which is midway through a level, which you might not want. For example, if "section2" starts on frame 100, and "section3" starts on frame 120, what happens if the player tries to save the game on frame 115?
So the third approach might be a better one:
ActionScript Code:
// In frame 1 create the shared object
var so:SharedObject = SharedObject.getLocal("user_location");
// read the attributes saved in the Shared Object
saved_level = so.data.savedLevel;
// Check to make sure it contains data. If not, e.g. if this is the first
// time the game is run, start on level 1
if (saved_level == undefined) {
    saved_level = 1;
}
//
// rest of your code, if any, goes here
//
// Now go to the saved level
gotoAndStop("section" + saved_level);
// generic function to save every level
function saveSO(level:Number):Void {
             // save the current_level that was passed into the function
    so.data.savedLevel = level;
             // and write the Shared Object to the disk
    so.data.flush;
}
//
// then, at the start of each level, add the following code, e.g.
// on frame "section1"
//
var current_level:Number = 1;
// pass the current level to the saveSO function
saveSO(current_level);
// rest of your code, if any, goes here
 

Continue in this fashion. "section2" would have the current_level = 2; and then you'd call the saveSO function..."section3" would be current_level = 3...and so on. And all you're saving in the Shared Object is a single number representing the current game level, instead of a list of every completed level so far!

As an added bonus, this code would automatically save each level without the need for a save button. But you can still place the saveSO(current_level) function call within a button handler if that's what you want. All you have to do is tell it what the current_level is.

One final suggestion with Shared Objects...although you've raised your local settings, bear in mind that:
a) a lot of people do panic when they see that dialog window open up, thinking that Flash is doing something mysterious to their PC, and
b) those that don't probably won't allow Flash to have more space.
So keeping your Shared Objects below the default level (or smaller) is best.

Hope that helps.

__________________
©2006 GlosRFC - Searching 8,168,684,336 brain cells

Last edited by glosrfc; 11-09-2009 at 11:15 PM.. Reason: Clarity! And all done now...pheew
glosrfc is offline   Reply With Quote
Old 11-09-2009, 11:05 PM   #5
Crystal
Registered User
Location San Diego, CA

Posts 36
Wonderful!! This is exactly what I have been looking for... Thank you SO much! Works perfectly. You just made my day....and my deadline.

Have a good one!
Crystal is offline   Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 03:51 PM.

SUPPORTERS:

kirupa.com's fast and reliable hosting provided by Media Temple. flash components
Creative web apps. Make your own free flash banners and photo slideshows.
Check out the great, high-quality flash extensions. Buy or sell stock flash, video, audio and fonts for as little as 50 cents at FlashDen.

Flash Transition Effects

Flash Effect Tutorials

Digicrafts Components
Flash effects. Art without coding. Upload, publish, deliver. Secure hosting for your professional or academic video, presentations & more. Screencast.com
Streamsolutions Content Delivery Networks Flipping Book - page flip flash component.
Flash-Gallery.com - Get your flash photo gallery (flash component or swf gallery Learn how to advertise on kirupa.com
 

cdn
content delivery network (cdn)

Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd. Copyright 2010 - kirupa.com Copyright 2010 - kirupa.com