Log in
Please log in or register.
Pages: [1] 2
  Print  
Author Topic: How-To: Adding a save feature to Flixel (and how to use it)  (Read 2928 times)
Merve
Newbie
*
Posts: 39


View Profile
« on: Sat, Sep 12, 2009 »

It's really, really easy. Go to the FlxG file and add a new import statement
Code:
// already here
import flash.display.BitmapData;
import flash.events.Event;
import flash.geom.Matrix;
import flash.geom.Point;
import flash.media.Sound;
import flash.media.SoundChannel;
import flash.media.SoundTransform;
import flash.net.SharedObject;
import flash.net.URLRequest;
import flash.net.navigateToURL;

//add this
import flash.net.SharedObject;

Now add a single line into the instance variables
Code:
//this stuff is already here
                //function reflectors
static private var _quake:Function;
static private var _flash:Function;
static private var _fade:Function;
static private var _switchState:Function;
static private var _log:Function;
static private var _setCursor:Function;

//add this line of code below it
static public var FlxSave:SharedObject = SharedObject.getLocal("userData");
This line of code will create a shared object if there isn't one there, and if there is one there it will copy it.

That's it. Your done, you now have a tiny little file sitting on everybody's computer who looks at your swf.

Now let me show you how to bend this file to your nefarious whims.

To access the file we go through FlxG into the FlxSave file and then we want to look at the data on the file, and then we specify the data name. Quite a long string but very easy to understand.

eg I have a variable called counter, I access it like this
Code:
FlxG.FlxSave.data.counter

Simple Smiley

There is only one more thing to note. When you are running your swf there will be two versions of FlxG.FlxSave. One is stored in your swf memory and one is stored on the computer. When we want to properly save data we simply use a function which says "Hey, computer, this is what that file should look like". This takes the file on the computer and makes it equal to the file in your swf memory.

Here's what it looks like
Code:
FlxG.FlxSave.flush();

Lets put this together into a little swf as an example: http://www.swfcabin.com/open/1252735529

Create a new swf with Flixel and add the pieces of code as above to the FlxG file and add the default cursor image, we'll want it later.

Now in the create a MenuState file, we create a little function which checks how many times this person has seen this swf, and increases to tally by 1.

Here's what it looks like
Code:
public function checksave():void
{
if (FlxG.FlxSave.data.counter == null)
{
FlxG.FlxSave.data.counter = 1;
} else {
FlxG.FlxSave.data.counter++;
}
FlxG.FlxSave.flush();
}

If the data we want isn't there, we create a new variable equal to 1. If it is there we increase the variable by 1. Then we save the data to the computer.

Now lets add a function to delete the saved information
Code:
public function wipesave():void
{
FlxG.FlxSave.data.counter = null;
FlxG.FlxSave.flush();
FlxG.switchState(MenuState);
}
We set the data to null, then save it to the computer.
Then we reload the state to reset the text. No text you say? Lets add some. Here's what the constructor looks like
Code:
public function MenuState():void
{
FlxG.setCursor(ImgCursor);
checksave();
this.add(new FlxText(0,80,FlxG.width,80,"You have visited this page\n" + FlxG.FlxSave.data.counter + " times",0xffffffff,null,16,"center"));
this.add(new FlxButton(120,160,new FlxSprite(null,0,0,false,false,80,16,0xff5f5f5f),wipesave,new FlxSprite(null,0,0,false,false,80,16,0xff898D8B),new FlxText(0,2,80,16,"Wipe data",0xffffff,null,8,"center"),new FlxText(0,2,80,16,"Wipe data",0xffffff,null,8,"center")));
}

We add a Cursor (so we can click the button), we check the save file, then we add some text saying how many times we've veiwed the page.
Lastly we add a button to wipe the data.

Here is the source file if you want to take a look: http://www22.zippyshare.com/v/2541909/file.html

Finally just some information. Each shared object is specific to a web address, that's why if you play a game on two different sites you will have 2 different saves.
Also shared objects are located outside the cookies folder, so they can only be deleted if someone really wants to delete then (you have to go through flash preferences)
You can stash pretty much any information you want to there, strings, number, arrays etc

I've probably missed one or two things, feel free correct me Wink
« Last Edit: Sat, Sep 12, 2009 by Merve » Logged
nitram_cero (2bam)
Sr. Member
****
Posts: 473



View Profile WWW
« Reply #1 on: Sat, Sep 12, 2009 »

Rad man!
I used shared objects before but in a sort of "black box" copy-paste.
This is a much better explanation, integration Smiley

One suggestion is to make the name of the shared object different according to the game's name/version (if not perhaps a whole lot of flixel games would start to read each other's info! like if you post your game to swfcabin, even if the shared object is sitelocked it would read the info from another Smiley)

Regards
Logged

2BAM
I Hate Islands, SpaceCoffee, Finding Her, Caverns, Explosive Cats, The Duke, Run, A View on Relationships, RabbitClock's Quest
Merve
Newbie
*
Posts: 39


View Profile
« Reply #2 on: Sat, Sep 12, 2009 »

Actually the saves are more than site locked, they are url locked (I believe).

I re-posted the same swf here: http://www.swfcabin.com/open/1252772591

If you flick between the counters you will see they are each independant.

The wikipedia page shows the location path for mac, os x and linux respectively so you can go and poke around the saves (.sol files) on your computer. I have a ridiculous amount of them, because they never get wiped and I love flash games Wink : http://en.wikipedia.org/wiki/Local_Shared_Object
Logged
nitram_cero (2bam)
Sr. Member
****
Posts: 473



View Profile WWW
« Reply #3 on: Sat, Sep 12, 2009 »

Awesome! Thanks for the info and the link Smiley
Logged

2BAM
I Hate Islands, SpaceCoffee, Finding Her, Caverns, Explosive Cats, The Duke, Run, A View on Relationships, RabbitClock's Quest
axcho
Jr. Member
**
Posts: 82



View Profile WWW
« Reply #4 on: Fri, Sep 18, 2009 »

Maybe this could be added to the next public release of Flixel? Smiley

You might change the name though, to match the other public static properties of FlxG. Maybe just call it FlxG.save, instead of FlxG.FlxSave.

Or FlxG.saved, to make it clear that it is a noun, a property, not a method.

FlxG.saved.data...
« Last Edit: Sun, Sep 20, 2009 by axcho » Logged
Merve
Newbie
*
Posts: 39


View Profile
« Reply #5 on: Sun, Sep 20, 2009 »

It's up to adam whether he wants to include it, it's just one line so it's fairly simple for even newbies to pop it in themselves.

I called mine FlxSave to make it clear that is an object, but saved works too  Wink
Logged
nitram_cero (2bam)
Sr. Member
****
Posts: 473



View Profile WWW
« Reply #6 on: Wed, Sep 23, 2009 »

If you allow me I will add this functionality to B-Flixel (But as FlxG.saved)

Tell me what you think

Thanks
-Martín
Logged

2BAM
I Hate Islands, SpaceCoffee, Finding Her, Caverns, Explosive Cats, The Duke, Run, A View on Relationships, RabbitClock's Quest
Merve
Newbie
*
Posts: 39


View Profile
« Reply #7 on: Wed, Sep 23, 2009 »

Fine with me (I think adobe actually holds the patent on my method  Wink)
Logged
nitram_cero (2bam)
Sr. Member
****
Posts: 473



View Profile WWW
« Reply #8 on: Thu, Sep 24, 2009 »

Hehe, right Wink

Thanks!!
Logged

2BAM
I Hate Islands, SpaceCoffee, Finding Her, Caverns, Explosive Cats, The Duke, Run, A View on Relationships, RabbitClock's Quest
Titch
Sr. Member
****
Posts: 251


Thing with the guy in the place.


View Profile
« Reply #9 on: Mon, Oct 19, 2009 »

So I've been dicking around with the SharedObject in flash because I was having trouble getting it to record objects correctly, since it casts them down to regular objects. So it might be a good idea to add some kind of support for using registerClassAlias to save Flx based objects (like FlxCore, or a custom save game class).
Logged

Free cake whippings every day at #flixel on irc.freenode.net.
nitram_cero (2bam)
Sr. Member
****
Posts: 473



View Profile WWW
« Reply #10 on: Tue, Oct 20, 2009 »

Take care with that because you're saving a lot of extra useless info, and by default flash allows you no more than about 1500 bytes of saved data.

Use it with care
Logged

2BAM
I Hate Islands, SpaceCoffee, Finding Her, Caverns, Explosive Cats, The Duke, Run, A View on Relationships, RabbitClock's Quest
CosMind
Newbie
*
Posts: 6

stuff 'n stuff


View Profile WWW
« Reply #11 on: Fri, Nov 13, 2009 »

good shot, Merve - much appreciated!  works marvelously.

one thing i'm still stumped on, though:
how might i save multiple datum - a separate score for each different level in a game, for example?
(without having to create and do a case check for umpteen individual FlxG._save.data.level#score variables...)

is there some way to build and write/access an array with this method?
« Last Edit: Fri, Nov 13, 2009 by CosMind » Logged
nitram_cero (2bam)
Sr. Member
****
Posts: 473



View Profile WWW
« Reply #12 on: Fri, Nov 13, 2009 »

1500 bytes of saved data.

I was mistaken, I think it stores 100k by default.
But take into account that perhaps (probably) it saves it as a JSON string, so the size could increase depending on the number of properties you save.

According to the docs, if more space is needed, a dialog will popup to ask the user if it allows it.

Also you can check .size (uint) on the shared object to draw some stats on the size of what you're saving, in bytes.

Regards!
« Last Edit: Fri, Nov 13, 2009 by nitram_cero » Logged

2BAM
I Hate Islands, SpaceCoffee, Finding Her, Caverns, Explosive Cats, The Duke, Run, A View on Relationships, RabbitClock's Quest
increpare
Newbie
*
Posts: 18


View Profile
« Reply #13 on: Mon, Nov 16, 2009 »

Thanks for this post: I will make use of it Cheesy

S
Logged
Hideous
Newbie
*
Posts: 48


View Profile
« Reply #14 on: Tue, Nov 17, 2009 »

I'm probably just going to save numbers and check for them in the classes. That'll be a lot easier, I believe. Cheesy
Logged
Pages: [1] 2
  Print  
 
Jump to: