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

前端 未结 1 642
醉酒成梦
醉酒成梦 2020-12-20 10:52

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 an

相关标签:
1条回答
  • 2020-12-20 11:13

    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;
                    }
                }
            }
       }
    }
    
    0 讨论(0)
提交回复
热议问题