Reputation: 221
I have 2 questions directed to Dictionary
1st : Im having a problem with saving data from Dictionary / loading data to Dictionary and i cant see why this isnt working.What am i doing wrong here???
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.events.MouseEvent;
import flash.utils.Dictionary;
import flash.net.SharedObject;
public class Main extends MovieClip
{
private var _box:Dictionary = new Dictionary ;
private var _change:Dictionary = new Dictionary ;
private var _loadingItems:Dictionary = new Dictionary ;
private var sData:SharedObject = SharedObject.getLocal("octopod_4");
public function Main()
{
if (sData.data.myStorage == undefined)
{
trace("1st time this game");
for (var i:int = 0; i < 5; i++)
{
var myImage_mc:MovieClip = new MovieClip();
_box[i] = myImage_mc;
if ( i > 1 )
{
_change[myImage_mc] = _box[i - 2];
}
}
sData.data.myStorage = _change;
sData.flush();
trace(sData.data.myStorage);
//we trace what we have in the Dictionary
for (var key:* in _change)
{
trace('_conditions[' + key.name + '] = ' + _change[key].name);
}
for (var key:* in _box)
{
trace('_box[' + key + '] = ' + _box[key].name);
}
}
else if ( sData.data.myStorage != undefined )
{
trace("loaded before");
_loadingItems = sData.data.myObject;
trace("_loadingitems " + _loadingItems)
}
}
}
2nd question: how can i extract that data from sData.data.myObject; and input it to _loadingItems What i mean is that because of the error it gives me Error #2004: One of the parameters is invalid. i cant see if the data from the sData.data.myObject; is copied to _loadingItems. Is it ??
Upvotes: 0
Views: 322
Reputation: 18747
You are putting MovieClips in your dictionary, and nothing extending DisplayObject
can be saved in a shared object unless you implement IExternalizable
for them. This is because any display object has publicly available stage reference which is dynamic, that is, the value stored in stage
property is a pointer to a memory location. Then, when you are trying to load an object out of a shared object, the behavior by default is to set all the properties to stored values which are now invalid because Flash occupies a different set of memory addresses each time a movie is loaded, and you get your 2004. IExternalizable
implementation overrides the default behavior with custom code, but you have to comply with certain hard restrictions when you write its implementation. So, if you want to store something in a shared object, you should devise a way to store only metadata that is somewhat static (strings, numbers, arrays are fine, but nested objects usually aren't, note that an object as a member of the array is a nested object too), and then create a set of dynamic objects (display objects of all sorts) according to retrieved metadata.
Upvotes: 1