Using 'File' to save scene object locations to rebuild later in AS3

后端 未结 5 1513
难免孤独
难免孤独 2020-12-02 03:20

I am attempting to use the \'File\' function in ActionScript 3 to save the following information:

I have varying draggable display objects in the scene, the amount a

5条回答
  •  日久生厌
    2020-12-02 03:38

    Assuming all your objects to save belong to the same parent, you could dosomething along these lines:

    First, create a class file (let's call is SaveData.as and put it in the root of your project directory). This will describe the data you want to save:

    package 
    {
        import flash.geom.Rectangle;
    
        public class SaveData 
        {
            public var bounds:Rectangle; //to save where an object is on the stage
            public var classType:Class; //to save what kind of object it is
    
            //you could add in more proterties, like rotation etc
    
    
            public function SaveData() {
    
            }
        }
    }
    

    Next, on your save function, do something like this:

        //this will hold all your data
        //a vector is the same as an array only all members must be of the specified type
        var itemList:Vector. = new Vector.();
    
        //populate the array/vector with all the children of itemContainer
        var tmpItem:SaveData;
        //loop through all children of item container
        for (var i:int = 0; i < itemContainer.numChildren; i++) {
            tmpItem = new SaveData(); //create a new save record for this object
            tmpItem.bounds = itemContainer.getChildAt(i).getBounds(itemContainer); //save it's bounds
            tmpItem.classType = getDefinitionByName(itemContainer.getChildAt(i)) as Class; //save it's type
            itemList.push(tmpItem);  //add it to the array
        }
    
        //Now you have an array describing all the item on screen
    
        //to automatically serialize/unserialize, you need this line (and you need to register every class nested in SaveData that isn't a primitive type - which would just be Rectangle in this case
        registerClassAlias("SaveData", SaveData);
        registerClassAlias("flash.geom.Rectangle", Rectangle);
    
        //create a new File to work with
        var file:File = File.applicationStorageDirectory; //or whatever directory you want
        file.resolvePath("saveData.data"); //or whatever you want to call it
        var fileStream:FileStream = new FileStream();
        fileStream.open(file, FileMode.WRITE);
        fileStream.writeObject(itemList); //write the array to this file
        fileStream.close();
    

    Now, to load it back in:

        var itemContainer:Sprite = new Sprite(); //however you initialize this
        addChild(itemContainer);
    
        var file:File = File.applicationStorageDirectory;
        file.resolvePath("saveData.data");
        var fileStream:FileStream = new FileStream();
        fileStream.open(file, FileMode.READ);
        var itemList:Vector. = fileStream.readObject() as Vector.;
        fileStream.close();
    
        //now that you've read in the array of all items from before, you need to recreate them:
        var tmpItem:DisplayObject;
        var tmpClass:Class;
        //loop through all items in the array, and create a object
        for (var i:int = 0; i < itemList.length; i++) {
            tmpClass = itemList[i].classType; //The type of item
            tmpItem = new tmpClass() as DisplayObject; //create the item
    
            //now move the item to it's former position and scale
            tmpItem.x = itemList[i].x;
            tmpItem.y = itemList[i].y;
            tmpItem.width = itemList[i].width;
            tmpItem.height = itemList[i].height;
    
            //add the item back to the parent
            itemContainer.addChild(tmpItem);
        }
    

    If you're not sure of the imports, here they are:

    import flash.filesystem.File;
    import flash.filesystem.FileMode;
    import flash.filesystem.FileStream;
    import flash.net.registerClassAlias;
    import flash.utils.getDefinitionByName;
    import flash.utils.getQualifiedClassName;
    

提交回复
热议问题