加载保存的位图

时间:2013-07-05 15:03:39

标签: actionscript-3 flash

我保存了一个位图,我想在运行时加载它。

这是我的代码:

  

var saveDataTxt:SharedObject = SharedObject.getLocal(“File”);

     

var textName:String; var textClass:Class;

     

textName =“Text0”+ 1;   textClass = getDefinitionByName(textName)as   类;
  var tx:BitmapData = new textClass(); txtP [1] =新的   位图(TX);

     

saveDataTxt.data.txtArray [1] = txtP [1];

     

的addChild(saveDataTxt.data.txtArray [N]);

但它给了我一个错误:

**TypeError: Error #1034: Type Coercion failed: cannot convert Object@384c2b1 to flash.display.DisplayObject.**

解决方案是什么?

1 个答案:

答案 0 :(得分:1)

要在共享对象中存储位图,首先需要将其序列化为字节数组(参见Is it possible to store images in the SharedObject of Flash?

您可以做的是将自定义BitmapData子类存储在共享对象中(如果您不想打扰字节数组)

//you need to register every class/subclass in your shared object
registerClassAlias("flash.display.BitmapData", BitmapData);


var saveDataTxt:SharedObject = SharedObject.getLocal("File");

var textName:String; var textClass:Class;

textName = "Text0" + 1; textClass = getDefinitionByName(textName) as Class;
registerClassAlias(textName,textClass); //need to register the custom class

var tx:BitmapData = new textClass(); txtP[1] = new Bitmap(tx);

saveDataTxt.data.txtArray[1] = tx; //just store the bitmap data

addChild(new Bitmap(saveDataTxt.data.txtArray[n] as BitmapData)); //you have to cast the object as bitmap data