Search code examples
actionscript-3flash

How do I save a dynamic text?


I have a dynamic text field that changes from 100 to 0 and vice versa when a button is clicked. I want this number to be saved when exiting the application, but it seems that it's not returning the last clicked value when the application reopens. This is the code, any help please?

import flash.events.MouseEvent;
import flash.media.SoundChannel;
import flash.ui.Mouse;

var saveDataObject:SharedObject;
var currentScore:Number;
options_mc.sound_btn.addEventListener(MouseEvent.CLICK, mute);

options_mc.test3.addEventListener(MouseEvent.CLICK, test3);
init();
function mute(event:MouseEvent)
{

if(currentScore == 100)
{
currentScore = 0
options_mc.onoff_txt.text = String(currentScore);

}
else if(currentScore == 0)
{
currentScore = 100
options_mc.onoff_txt.text = String(currentScore);
}
saveData();
}
function init():void
{

saveDataObject = SharedObject.getLocal("test");
currentScore = 100;




if (saveDataObject.data.savedScore == null)
{
    trace("No saved data yet.");
    saveDataObject.data.savedScore = currentScore;
}
else
{
    trace("Save data found.");
    loadData();
}


}

function saveData():void
{
saveDataObject.data.savedScore = currentScore;
trace("Data Saved!");
saveDataObject.flush();
trace(saveDataObject.size);
}

function loadData():void
{
if(currentScore == 100)
{
currentScore = saveDataObject.data.savedScore;
trace("Data Loaded!");
}
else if(currentScore == 0)
{

    currentScore = saveDataObject.data.savedScore;
}
}

Solution

  • function loadData():void
    {
        currentScore = saveDataObject.data.savedScore;
        trace("Data Loaded!");
        if (options_mc.onoff_txt) options_mc.onoff_txt.text=String(currentScore);
    }
    

    Apparently you've missed the line that immediately writes the score to text field. You are seemingly doing the saving right.