Flash AS3:保存/加载MovieClip的所有子项的位置 [英] Flash AS3: saving/loading the positions of all the children of a MovieClip

查看:386
本文介绍了Flash AS3:保存/加载MovieClip的所有子项的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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



它们保存并加载MovieClip的孩子的X和Y位置。 code> save.addEventListener(MouseEvent.CLICK,fl_MouseClickHandler3);

函数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);

函数fl_MouseClickHandler_2(event:MouseEvent):void
{

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


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




$ p
$ b

然而,这只保存并加载最后点击的MovieClip子元素。
如何从只保存和加载最后一次点击的MovieClip子元素
来保存和加载所有的MovieClip子元素?

解决方法

这将总是保存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);

函数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)作为MovieClip;
if(child)
{
clippositions.push({clipname:child.name,my_x:child.x,my_y:child.y});



mySo.data.clippositions = clippositions
mySo.flush();


函数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)作为MovieClip;
if(child)
{
child.x = clippositions [i] .my_x;
child.y = clippositions [i] .my_y;





code $ pre

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?

解决方案

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

这篇关于Flash AS3:保存/加载MovieClip的所有子项的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆