如何使用as3在Flash游戏中保存和加载最新的电影剪辑?

时间:2017-07-18 06:16:10

标签: actionscript-3

我有很多使用" addChild / removeChild"添加或删除的动画片段。当玩家浏览游戏视图时。我想保存这些影片剪辑(可以使用共享对象),因此加载函数显示视图的最后状态。我不得不说每个视图中总是有多个影片剪辑。我还没有具体的代码。

1 个答案:

答案 0 :(得分:1)

我不建议您将对象完全存储在SO中。仅保存对象配置数据。 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);
    }
}
}

被序列化SomeObject类 - 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);
    }
}
}