0

我正在尝试加载一个包含一些显示对象的数组,该程序允许我用圆圈填充数组并将它们保存到共享对象,然后,我可以使用加载按钮跟踪我的数组的内容。问题是我重新启动程序后无法加载数组。它跟踪我这条消息:“加载的对象:,,”

这是代码:

var SO:SharedObject=SharedObject.getLocal("myFile", "/");

var arr:Array=new Array();

var counter:Number=-1;

addBtn.addEventListener(MouseEvent.CLICK, addObjects);

saveBtn.addEventListener(MouseEvent.CLICK, saveObjects);

loadBtn.addEventListener(MouseEvent.CLICK, loadObjects);

function addObjects(event:Event) {

    counter++;

    var circle:circleClip=new circleClip();

    arr.push(circle);

    trace("current object: "+arr[counter]);
}

function saveObjects(event:Event) {

    SO.data.arrSaved=arr;

    SO.flush();

    trace("objects saved: "+SO.data.arrSaved);
}

function loadObjects(event:Event) {

    var arrLoaded:Array=new Array();

    arrLoaded=SO.data.arrSaved;

    trace("objects loaded: "+arrLoaded);
}
4

1 回答 1

0

您要了解 MVC 模式方法:https ://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller

您需要将数据与将这些数据可视化的代码分开,并仅存储数据。就像是:

// Lets say this Array contains a list of your circle objects.
var Curcus:Array;

// Storage.
var SO:SharedObject = SharedObject.getLocal("myFile", "/");

function saveCircus():void
{
    var aList:Array = new Array;

    aList.length = Circus.length;

    for (var i:int = 0; i < Curcus.length; i++)
    {
        // Get an object.
        var aCircle:Sprite = Curcus[i];

        // Record its properties you want to store.
        var anEntry:Object =
        {
            "x":aCircle.x,
            "y":aCircle.y,
            "scaleX":aCircle.scaleX,
            "scaleY":aCircle.scaleY
        };

        // Store the entry.
        aList[i] = anEntry;
    }

    // Store and save data.
    SO.data.arrSaved = aList;
    SO.flush();
}

function loadCircus():void
{
    // Retrieve saved data.
    var aList:Array = SO.data.arrSaved;

    // Make it empty data if there are none.
    if (!aList) aList = new Array;

    Circus = new Array;
    Curcus.length = aList.length;

    for (var i:int = 0; i < aList.length; i++)
    {
        // Get one entry.
        var anEntry:Object = aList[i];

        // Create a new item. BTW, naming classes with the
        // lowercase first letter is the 8th cardinal sin.
        var aCircle = new CircleClip;

        // Restore saved properties.
        aCircle.x = anEntry['x'];
        aCircle.y = anEntry['y'];
        aCircle.scaleX = anEntry['scaleX'];
        aCircle.scaleY = anEntry['scaleY'];

        // Add to display list.
        addChild(aCircle);

        // Keep it for the future reference/saving.
        Curcus[i] = aCircle;
    }
}
于 2017-04-28T23:29:24.180 回答