Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
650 views
in Technique[技术] by (71.8m points)

actionscript 3 - Flash AS3: saving/loading the positions of all the children of a MovieClip

I would like to know how to save and load the X and Y position of all the children of a MovieClip.

I have a project with a save and load button.

They save and load the X and Y position of the MovieClip's child.

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;

}

This however only saves and loads the last clicked MovieClip child. How can I change it from only saving and loading the last clicked MovieClip child, to saving and loading all the MovieClip children?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

That will always save "mc2" positions, you will need to run a loop on numchildrens of the movieclip and put their positions in an array, and then access them in the same way. Here is a code example

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;
                }
            }
        }
   }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...