actionscript-3flashactionscriptadobe-animate

how to load dynamic text sharedObject when flash is reopened in Actionscript 3?


I created a simple username page, in frame 1, there is a button and input text

like the code below

stop();
var SOlastFrame: SharedObject = SharedObject.getLocal("save_frame2");
var shared_data: String

next.addEventListener(MouseEvent.CLICK, gotomyNextFrame);

function gotomyNextFrame(e: MouseEvent): void {
    shared_data = inputName.text

    nextFrame()
    SOlastFrame.data.lastframe = currentFrame;
    SOlastFrame.flush();

}

if (SOlastFrame.data.lastframe != null) {
    gotoAndStop(SOlastFrame.data.lastframe);
}

i saved the last frame by adding this code

SOlastFrame.data.lastframe = currentFrame;
SOlastFrame.flush();

so that I can jump to the last frame I opened, I added this code

if (SOlastFrame.data.lastframe != null) {
    gotoAndStop(SOlastFrame.data.lastframe);
}

at frame 2, I put dynamic text with code like this

var SOnameUser: SharedObject = SharedObject.getLocal("saveName");

SOnameUser.data.yourName = shared_data;
SOnameUser.flush();
trace(SOnameUser.data.yourName);

userName.text = "Hello " + shared_data;

if (shared_data != null) {
    userName.text = shared_data;
    SOnameUser.data.yourName = shared_data;
}

I think it works, dynamic text is saved successfully. If I reopen it, it will go directly to frame 2 because of the execution result SOlastFrame.data.lastframe.

the problem is that the username that I saved earlier turns to null.

how can i load dynamic text on shareobject when SOlastFrame.data.lastframe is executed.

or

how to load sharedObject when flash is reopened


Solution

  • You get it like that because when you start 2-nd time you go straight to the frame 2 where the shared_data is empty. I think you should re-organize... well, everything.

    // Frame 1: Splash (you don't have it).
    // First of all, use a single SO rather than two.
    var SO:SharedObject = SharedObject.getLocal("my.save");
    
    // Check if there are saved fields.
    if (SO.data.userName && SO.data.lastFrame)
    {
        // If there are saved credentials, the user
        // won't even see the Login Frame.
        gotoAndStop(SO.data.lastFrame);
    }
    else
    {
        // If there are no saved credentials — proceed to the Login Frame.
        nextFrame();
    }
    

    Then, this is your frame 1.

    // Frame 2: Login.
    
    stop();
    
    // You don't actually need to SO.flush() every time,
    // it's a (feeble) measure against sudden crashes.
    SO.data.lastFrame = currentFrame;
    
    // Your code, for the most part, but simpler.
    next.addEventListener(MouseEvent.CLICK, gotoNext);
    
    function gotoNext(e:MouseEvent):void
    {
        // You don't need any additional variables
        // to temporarily store the user name.
        SO.data.userName = inputName.text;
        
        nextFrame();
    }
    

    Then, here we go.

    // Frame 3: only logged (via Login Frame or via SO data)
    // users reach this point.
    
    stop();
    
    // In case we moved from the Login Frame.
    SO.data.lastFrame = currentFrame;
    
    // The only thing left to do.
    userName.text = "Hello " + SO.data.userName;