-2

I have many movieclips that i add or remove with "addChild / removeChild" as player navigate through the game views. I want to save these movie clips (propably using shared objects) so load function show the last status of view. I have to say that always are more than one movie clip in each view. I have no a specific code for this yet.

  • That doesn't make any sense. Why don't you just store the data and then recreate view based on that? – 3vilguy Jul 18 '17 at 09:22
  • 1
    https://stackoverflow.com/questions/30125221/using-file-to-save-scene-object-locations-to-rebuild-later-in-as3/30131304#30131304 – Organis Jul 18 '17 at 10:02
  • Do you mean one by one? that's i'm trying to avoid but i never used saving loading before. – newUser Jul 18 '17 at 10:41
  • A, maybe i should look more closely at the link you gave me, although most answers there are related to air. – newUser Jul 18 '17 at 11:21
  • If you want to lead a conversation, you need to mark your messages with @organis so I receive the notifications. And correct, if you want to save/restore some data between your application sessions you need to write a custom methods that save and recover **data** to and from **SharedObject** (or maybe remove server) with the structure and logic of your application in mind. – Organis Jul 18 '17 at 14:26
  • @organis i have seen Marty's example stackoverflow.com/questions/6938646/how-to-save-and-load-the-latest-movie-clips-in-a-flash-game-with-as3#. I tried the first part on safe function and it seems that maybe works for me because trace outputs all displayed movie clips i have in each view. If it;s true could you please help me with load function? Thank you for your patience! – newUser Jul 18 '17 at 16:13
  • There's no save function on that link. Try this one: https://stackoverflow.com/questions/35914538/flash-as3-saving-loading-the-positions-of-all-the-children-of-a-movieclip – Organis Jul 18 '17 at 16:32
  • @organis sorry i forgot to say that i have added a save function in this ''sharedObject.data.mcData = e;'' – newUser Jul 18 '17 at 16:38
  • Oh, very good. Now please explain the meaning of that operation. – Organis Jul 18 '17 at 16:44
  • I;m trying to save every displayed movie clip added last (so the last view) when user presses the save button (only these have been added with addChild of course, other elements are nested inside them). When user load the game will entry in the view that had pressed ''save''. Thank you again for your time! – newUser Jul 18 '17 at 16:58
  • Please carefully read this (http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/net/SharedObject.html#data) to learn why you cannot do it the way you did it. – Organis Jul 18 '17 at 17:05
  • @organis it seems that you have an answer for everything. I will check the previous link you suggest – newUser Jul 18 '17 at 17:11
  • 1
    Welcome to StackOverflow : ))) I will not provide you with the code, but I will help you understand how to write it on your own. That's how it works here. – Organis Jul 18 '17 at 17:19
  • @organis you helped me a lot (as you understand i'm not a programmer), thanks again! – newUser Jul 18 '17 at 17:23

1 Answers1

1

I do not advise you to store objects entirely in SO. Save only object config data. Adobe Flash Professional CS6 (v. 12).

Main - SharedObjectSaver.as

package  {

import flash.display.MovieClip;
import flash.net.SharedObject;
import flash.utils.ByteArray;
import flash.net.registerClassAlias;

public class SharedObjectSaver extends MovieClip 
{
    public function SharedObjectSaver() 
    {
        //Step 1. Check object
        //this.addChild(new SomeObject(20,100));

        //Step 2. Save object to SharedObject file.
        registerClassAlias("SomeObject", SomeObject);
        var mySo2:SharedObject  = SharedObject.getLocal("SaveData");
        mySo2.data.someObject   = new ByteArray();
        mySo2.data.someObject.writeObject(  new SomeObject()    );              
        mySo2.flush();

        //Step 3. Try to read object from SharedObject file.
        registerClassAlias("SomeObject", SomeObject);
        var mySo3:SharedObject  = SharedObject.getLocal("SaveData");
        mySo3.data.someObject.position = 0;
        var someObjectFromSO:* = mySo3.data.someObject.readObject() as SomeObject;
        trace(someObjectFromSO is SomeObject); // true

        this.addChild(someObjectFromSO);
        trace(someObjectFromSO.s);
    }
}
}

Being serialized SomeObject class - SomeObject.as

package  {

import flash.display.Sprite;
import flash.display.Shape;
import flash.display.Loader;
import flash.net.URLRequest;

public class SomeObject extends Sprite 
{
    public var s:String;

    // You can not require passing arguments to the constructor 
    // because ByteArray in readObject() method tries to read class and 
    // create an instance of serialized object.

    public function SomeObject() 
    {
         var loader:Loader = new Loader();
         loader.load(new URLRequest('https://cdn.sstatic.net/Sites/stackoverflow/company/img/logos/so/so-logo.png')); 

        s = "Hi";
        trace("I'm called");

        var rectangle:Shape = new Shape;
        rectangle.graphics.beginFill(0xFF0000);
        rectangle.graphics.drawRect(0, 0, 10,20);
        rectangle.graphics.endFill();
        this.addChild(rectangle);

        this.addChild(loader);
    }
}
}
  • Thank you for this. It seems difficult for me, so i need time to understand and try it in a project. – newUser Jul 18 '17 at 19:27