1

I'll explain a little before asking my question ... I've created numerous games which load and unload off a main menu.

The player enters their name on the main menu before playing any games and when the player completes a game I want to save their time (taken to complete the game) and unload this time back into the main menu.

Is there any way of saving the times using AS3 to a word document or something like this? I can't send the times to my website with php because the games will be used within a competition and it all needs to work with the internet.

Any ideas guys?

Edit:

    var dataloader:URLLoader = new URLLoader();
    var dataarray:Array = new Array(); // do this where you intialise other vars

    function preparesave() 
    {
        dataloader.load(new URLRequest("savedata.txt"));
    }

    dataloader.addEventListener(Event.COMPLETE,savedata);

    function savedata (event:Event) 
{
    dataarray = event.target.data.split(/\n/);
    dataarray.push(MyTimer);

    var bytes:ByteArray = new ByteArray();
    var fileRef:FileReference = new FileReference();

    for (var i = 0; i < dataarray.length;i++) 
    {
        bytes.writeMultiByte(dataarray[i] + "\n", "iso-8859-1");
        bytes.writeMultiByte('English Game Time: ',"iso-8859-1");
        bytes.writeMultiByte(HourText.text.toString(),"iso-8859-1");
        bytes.writeMultiByte(':',"iso-8859-1");
        bytes.writeMultiByte(MinuteText.text.toString(),"iso-8859-1");
        bytes.writeMultiByte(':',"iso-8859-1");
        bytes.writeMultiByte(SecondText.text.toString(),"iso-8859-1");
    }

    fileRef.save(bytes,"savedata.txt")
}
Ant
  • 77
  • 2
  • 10
  • 4
    Can you clarify the last sentence? Because sending scores via php sounds like "working with the internet" after all. – joncys Apr 08 '12 at 15:25

2 Answers2

2

You could use a cookie(sharedObject) for this purpuse. See http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/net/SharedObject.html

var data:SharedObject = SharedObject.getLocal("userdata");
    data.startTime = new Date().time;

If you need the startTime again you could just retrieve it by the cookie and variable name.

automaticoo
  • 868
  • 7
  • 24
1

For small amounts of data, you're best-off just going with automaticoo's solution by using SharedObjects.. they take minimal time to set up and are an efficient way of storing small amounts of data.

However, if it's not for you, you could always load/save to a text file. You can do this using ByteArrays and FileReferences.

Since you brought up saving to a word file, I would suggest saving to a text file would be the best way of achieving your goal (although other than your word doc. suggestion, i'm not sure.. your aim is sort of unclear)

Here is a seriously quick demonstration of saving to a text file.. If you need help loading it too, let me know.

function savedata() {
var bytes:ByteArray = new ByteArray();
var fileRef:FileReference = new FileReference();
bytes.writeMultiByte(playername + "\n", "iso-8859-1");
bytes.writeMultiByte(playerscore.toString(),"iso-8859-1");
fileRef.save(bytes,"savedata.txt");
}

This is pretty simply.. By using writeMultiByte, we can write as much data as needed before saving our text file! This is especially useful if you have arrays of data you need to save.

Anyway, the "iso-8859-1" is simply a reference to the character set / file-format being used.. I'm not sure if you can simply write utf-16 etc. instead.. I've always just used it as it is written above.

The result in the text file here will be the following:

Peter

9547 (but up one line)

To load, you can just split data by line, resulting in an array full of strings and ints/numbers (however your scores may work). Again, let me know if you need help doing that.

I'm not sure if this is the most efficient method of achieving what you're after since your goal isn't entirely clear to me, but it has been a very useful way for myself in storing large amounts of data, which I'm guessing you are wanting to do if you are compounding all the scores of players.

edit:

Okay, to the questions you posed below:

  1. To save more than one user's score is simple. Simply have a single array which loads the data from the text file (if the text file exists), then adds the new score to the end of the array, then saves back to the text file.

Here is an example:

var dataloader:URLLoader = new URLLoader();
var dataarray:Array = new Array(); // do this where you intialise other vars

function preparesave() {
    dataloader.load(new URLRequest("savedata.txt"));
}

dataloader.addEventListener(Event.COMPLETE,savedata);
function savedata (event:Event) {
    dataarray = event.target.data.split(/\n/);
    dataarray.push(playername);
    dataarray.push(playerscore);
    var bytes:ByteArray = new ByteArray();
    var fileRef:FileReference = new FileReference();
    for (var i = 0; i < dataarray.length;i++) {
        bytes.writeMultiByte(dataarray[i] + "\n", "iso-8859-1");
    }
    fileRef.save(bytes,"savedata.txt")
}

What this does is take any existing save data, pushes it to an array (where one line on your save file is one array data entry), adds your appropriate data, and pushes it back out to the save file.

(2) To load this in whilst in the main menu, simply place this code in the frame of the main menu..

dataloader.load(new URLRequest("savedata.txt"));
dataloader.addEventListener(Event.COMPLETE,loaddata);
function loaddata (event:Event) {
    dataarray = event.target.data.split(/\n/);
}

This will load all your existing data into an array. What you do from then on is up to you.

hazdog
  • 121
  • 1
  • 3
  • 20
  • Hey thanks for the reply. This code will save the players time at the end of the game and would need to be loaded once the finished game has been unloaded and the main menu re-loaded. I have a few problems now: The code asks for the user to save, could this be done automatically? If I wanted to save more than one users time's in the same txt file how could I do this? And finally how would I go about loading this in the main menu? Thanks for your help. – Ant Apr 09 '12 at 10:58
  • No worries buddy. Unfortunately, (but also fortunately), saving to a file can't physically be done without asking the user, as it is a security risk. It is not something we can achieve. As for your other questions, I will edit my answer above with some answers. :) – hazdog Apr 09 '12 at 13:07
  • Hey have edited my question at the top of the page to show what the code I have looks like. For some reason it doesn't bring up and error or not does it save anything? Sorry about this is problem me missing something. – Ant Apr 09 '12 at 15:27
  • Have tried to move the code around and place it like I did with the first version but no luck :\ Kind of odd how it wont work now but it works fine with the first version. – Ant Apr 09 '12 at 18:40
  • Have you called preparesave()? Because it isn't being called anywhere in that block of code. :) – hazdog Apr 09 '12 at 21:42
  • No how do I do this I thought it would get called automatically as it's in a funcation? – Ant Apr 09 '12 at 22:59
  • Just execute as preparesave(); in the frame you want to run it in. – hazdog Apr 10 '12 at 03:00
  • Got it working now ... Sort of ... It saves the information I asked for 3 times in the txt file which is odd. Also don't suppose you know what I'd code I enter in AS3 to drop down a space inside a txt file? – Ant Apr 10 '12 at 08:50
  • You can use "\n" to go down a line. If you have any other questions you may wish to message me or post a new question on stackoverflow. :) – hazdog Apr 10 '12 at 13:50