Flash AS3:保存/加载MovieClip的所有子项的位置

时间:2016-03-10 10:59:33

标签: actionscript-3 flash

我想知道如何保存和加载MovieClip的所有子项的X和Y位置。

我有一个带有保存和加载按钮的项目。

他们保存并加载MovieClip的孩子的X和Y位置。

save.addEventListener(MouseEvent.CLICK, fl_MouseClickHandler3);

function fl_MouseClickHandler3(event:MouseEvent):void
{

var mySo:SharedObject = SharedObject.getLocal("SaveData");

mySo.data.my_x = mc2.x;
mySo.data.my_y = mc2.y;
mySo.flush();


}

loader.addEventListener(MouseEvent.CLICK, fl_MouseClickHandler_2);

function fl_MouseClickHandler_2(event:MouseEvent):void
{

var mySo:SharedObject = SharedObject.getLocal("SaveData");


mc2.x = mySo.data.my_x;
mc2.y = mySo.data.my_y;

}

然而,这仅保存并加载最后点击的MovieClip子项。 如何更改它仅保存和加载最后一次单击的MovieClip子项, 保存和加载所有MovieClip孩子?

1 个答案:

答案 0 :(得分:0)

这将永远保存“mc2”位置,你需要在movieclip的numchildrens上运行一个循环并将它们的位置放在一个数组中,然后以相同的方式访问它们。这是一个代码示例

import flash.display.MovieClip;

var mySo:SharedObject = SharedObject.getLocal("SaveData");
save.addEventListener(MouseEvent.CLICK, fl_MouseClickHandler3);
loader.addEventListener(MouseEvent.CLICK, fl_MouseClickHandler_2);

function fl_MouseClickHandler3(event:MouseEvent):void
{
    var clippositions:Array = new Array();
    var child:MovieClip;
    for(var i:uint=0; i<this.numChildren; i++)
    {
        if( this.getChildAt(i) is MovieClip )
        {
            child = this.getChildAt(i) as MovieClip;
            if(child)
            {
                clippositions.push( { clipname:child.name, my_x:child.x,my_y:child.y } );
            }
        }
    }
    mySo.data.clippositions = clippositions
    mySo.flush();
}

function fl_MouseClickHandler_2(event:MouseEvent):void
{
    var clippositions:Array = mySo.data.clippositions;
    if( clippositions != null )
    {
        var child:MovieClip;
        for(var i:uint=0; i<clippositions.length; i++)
        {
            if( this.getChildByName( clippositions[i].clipname ) is MovieClip )
            {
                child = this.getChildByName( clippositions[i].clipname ) as MovieClip;
                if(child)
                {
                    child.x = clippositions[i].my_x;
                    child.y = clippositions[i].my_y;
                }
            }
        }
   }
}