简体   繁体   English

是否可以在关闭时不刷新共享对象?

[英]Is it possible not to flush sharedobject on close?

I'm having a problem with the sharedObject flush method. 我在sharedObject刷新方法方面遇到问题。 Is it possible to not flush the new data when the swf is closed? swf关闭时是否可以不刷新新数据? My savegame function is the only function that calls the flush method and it also determines which array goes where in the sharedObject data. 我的savegame函数是唯一调用flush方法的函数,它还确定哪个数组位于sharedObject数据中的何处。

parentMC.sharedObject.data.moveSpdUpgrade = parentMC.upgrades.tempMoveSpdUpgrade;
parentMC.sharedObject.flush();

However, when I modify the tempMoveSpdUpgrade array, it also saves the new data in the sharedObject even if the flush hasn't been called yet. 但是,当我修改tempMoveSpdUpgrade数组时,即使尚未调用刷新,它也会将新数据保存在sharedObject中。

tempMoveSpdUpgrade[0][2] = 1;
trace(parentMC.sharedObject.data.moveSpdUpgrade);

This trace shows that the data has changed but I don't understand since the flush hasn't been called and the swf hasn't been closed. 此跟踪表明数据已更改,但是我不明白,因为尚未调用冲洗并且尚未关闭swf。 I'm wondering why the modifications made in the array automatically changes the sharedObject data. 我想知道为什么在数组中进行的修改会自动更改sharedObject数据。

Thank you for the help. 感谢您的帮助。

Edit: 编辑:

public function saveGame(){  
    parentMC.sharedObject.data.money = parentMC.money;  
    parentMC.sharedObject.data.moveSpdUpgrade = parentMC.upgrades.tempMoveSpdUpgrade;  
    parentMC.sharedObject.flush();  
}

Like I stated in the comments with hackattack, the money is the correct data when I don't save but the moveSpdUpgrade array is modified either way. 就像我在hackattack的注释中所述,当我不保存钱时,钱是正确的数据,但是无论哪种方式都修改了moveSpdUpgrade数组。

What is in the sharedObject that is loaded into memory and what is saved to disc are separate things. 共享对象中加载到内存中的内容和保存到磁盘中的内容是分开的。 You are tracing the sharedObject in memory. 您正在跟踪内存中的sharedObject。 Until you flush this sharedObject it probably wont save its image to disc. 在刷新此sharedObject之前,它可能不会将其映像保存到磁盘。 Try creating a new SharedObject and tracing that, it should be different. 尝试创建一个新的SharedObject并进行跟踪,它应该有所不同。

Edit: 编辑:

I see an error in what i told you to do before (below in the comments). 我之前告诉过您的操作有误(在评论中)。 I told you to make a new instance of moveSpdUpgrade to place in your shared object with the .concat method. 我告诉您使用.concat方法创建一个新的moveSpdUpgrade实例,以放置在共享对象中。 The reason this doesnt work is because moveSpdUpgrade is a 2d array, i missed this detail. 这行不通的原因是因为moveSpdUpgrade是一个二维数组,我错过了这个细节。 So calling .concat creates a new instance, but the new instance is still filled with references to arrays and it those arrays that you are editing. 因此,调用.concat会创建一个新实例,但是新实例仍然充满了对数组及其正在编辑的数组的引用。 So to solve this we have to do a deep copy of moveSpdUpgrade. 因此,要解决此问题,我们必须对moveSpdUpgrade进行深层复制。 Here is a function that will accomplish that 这是一个可以完成此任务的功能

function array2DConcat(target : Array) : Array {
    var buff : Array = new Array(target.length);
    for(var i : int = 0;i < target.length;i++) }
        buff[i] = target[i].concat();
    }
    return buff;
}

and then if you change your method to 然后如果您将方法更改为

public function saveGame(){  
    parentMC.sharedObject.data.money = parentMC.money;  
    parentMC.sharedObject.data.moveSpdUpgrade = array2DConcat(parentMC.upgrades.tempMoveSpdUpgrade);  
    parentMC.sharedObject.flush();  
}

hopefully that will work. 希望这会起作用。

I think you are running into a bit confusing thing about arrays. 我认为您对数组感到有些困惑。 What is happening never saves any data by another function or has something to do with the rest of your code. 发生的事情永远不会通过其他函数保存任何数据,或者与代码的其余部分有关。

Simple the method how you try to copy the array of that Sharedobject doesnt create two seperate Obejcts. 您尝试复制该Sharedobject数组的简单方法不会创建两个单独的对象。 Instead you are always working live on the sharedObject-array. 相反,您始终在sharedObject-array上实时工作。

package
{
  import flash.display.Sprite;

  public class arraytester extends Sprite
  {

    private var a:Array = new Array(1,2,3,4,5);
    private var b:Array = new Array("a","b","c","d","e","f");

    public function arraytester()
    {

    // Both arrays are different Objects
        trace(a); // 1,2,3,4,5
        trace (b); //a,b,c,d,e,f

    // Both arrays are pointing to the same Object
        a= b;
        trace(a); //a,b,c,d,e,f
        trace (b); //a,b,c,d,e,f

        b[1] = 2;
        trace(a); //a,2,c,d,e,f
        trace (b); //a,2,c,d,e,f

    // a is a copy of b while becoming a seperate Object
        a = b.concat();
        b[0]= 1;
        trace(a); //a,2,c,d,e,f
        trace (b); //1,2,c,d,e,f
    }
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM