0

i had saved a bitmap and i want to load it in runtime.

here is my codes:

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

var textName:String; var textClass:Class;

textName = "Text0" + 1; textClass = getDefinitionByName(textName) as Class;
var tx:BitmapData = new textClass(); txtP[1] = new Bitmap(tx);

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

addChild(saveDataTxt.data.txtArray[n]);

but it gives me an error :

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

whats the solution?

Ali JJ
  • 21
  • 5
  • it seems like saveDataTxt.data.txtArray[n] isn't a displayobject, but just a regular object. What is stored in the sharedobject? – Marijn Jul 05 '13 at 15:23
  • i'm trying to save a bitmap in sharedobject – Ali JJ Jul 05 '13 at 15:47
  • You problem is likely that you need to register the class before loading back in. See my answer to this question: http://stackoverflow.com/questions/15667848/as3-for-ios-how-to-serialize-an-array-and-then-save-it/15668079#15668079 – BadFeelingAboutThis Jul 05 '13 at 16:37
  • i just used above codes. 'saveDataTxt.data.txtArray[1] = txtP[1];' – Ali JJ Jul 05 '13 at 18:05
  • Did you try doing: `registerClassAlias("flash.display.Bitmap", Bitmap);` `registerClassAlias("flash.display.BitmapData", BitmapData);` – BadFeelingAboutThis Jul 05 '13 at 19:04
  • also, you don't define `n` in the code shown, are you sure it's value is `1`? Why wouldn't you just do `addChild(txtP[1]);`? As it is, unless you're retrieving the bitmap somewhere else after the application is closed, there's no reason for you to be using sharedObject at all. – BadFeelingAboutThis Jul 05 '13 at 19:10
  • yeah, i wanna access to bitmaps after i close the application. – Ali JJ Jul 05 '13 at 20:23

1 Answers1

1

To store a bitmap in a shared object, you would need to serialize it to a byte array first (see Is it possible to store images in the SharedObject of Flash?)

What you can do, is just store your custom BitmapData subclass in the shared object (if you don't want to bother with byte arrays)

//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
Community
  • 1
  • 1
BadFeelingAboutThis
  • 14,445
  • 2
  • 33
  • 40
  • thank you very much, actually i'm using for loop for that. i cast those objects as bitmap but now there is another error that says: TypeError: Error #2007: Parameter child must be non-null. at flash.display::DisplayObjectContainer/addChild() – Ali JJ Jul 05 '13 at 20:11
  • i used trace and it shown me "object Object" without casting and "null" with casting as bitmap. i think i must save saveDataTxt.data.txtArray[1] = txtP[1] as bitmap. but how? – Ali JJ Jul 05 '13 at 20:20
  • But have you put in the flash.net.registerClasAlias calls yet? – BadFeelingAboutThis Jul 05 '13 at 21:10
  • You may need to register your custom classes too then. `Text01` or whatever they are called – BadFeelingAboutThis Jul 05 '13 at 21:46
  • i registered registerClassAlias("flash.display.textClass", textClass); and error is : TypeError: Error #2007: Parameter classObject must be non-null.at global/flash.net::registerClassAlias() – Ali JJ Jul 05 '13 at 21:53
  • Just tried it myself. Problem with bitmap is that the transform object needs parameters to be reconstructed. What you can do, is forget the bitmap and just store the bitmapData. updated my answer – BadFeelingAboutThis Jul 05 '13 at 21:56
  • You're probably registering the class before your populating that var. are you doing it like in my example? – BadFeelingAboutThis Jul 05 '13 at 22:02
  • i tried it and got me same error. my idea is assign to each bitmap a number and storing that numbers . but i dont know how can i do this. – Ali JJ Jul 05 '13 at 22:18
  • could you do me a favor. that codes is simplified . can i send you original source and you look at it and find the solution. i think you find the problem faster. thank you so much. – Ali JJ Jul 05 '13 at 22:22
  • If you edit your question to include a link to a source file I can take a look, though it may not be today. – BadFeelingAboutThis Jul 05 '13 at 22:26