[solved] Can we store a BitmapData inside a FlxSave?



  • Hi,

    I'm trying to save the BitmapData of a user inside a FlxSave so he don't have to re-import his stuff the next time he plays. I'm targeting Flash...
    The problem is that when I trace the BitmapData after adding it to the savefile, everything's fine, but when I load that savefile afterwards, the BitmapData is missing.

    Is that normal? Maybe we can only store Int and String in a savefile?
    If that so, is it possible for me to convert the BitmapData into a String in order to store it inside the savefile? And decode it later on...

    Thanks for your precious help !



  • I think you need to serialize and deserialize the BitmapData to save it.



  • You can store binary data in Base64 format, which is a string



  • Thanks @DleanJeans, that was the perfect hint :)

    I just had to convert the BitmapData to Bytes before serializing it. Here's what I did:

    import flixel.addons.util.PNGEncoder;
    import haxe.Serializer;
    import haxe.Unserializer;
    import flash.display.BitmapData;
    (...)
    	private function serialize(bmpData:BitmapData):String
    	{
    		var bytes:Bytes = Bytes.ofData(PNGEncoder.encode(bmpData));
    		var serializer = new Serializer();
    		serializer.serialize(bytes);
    		return serializer.toString();
    	}
    
    	private function unserialize(str:String):BitmapData
    	{
    		var bytes:Bytes = new Unserializer(str).unserialize();
    		var bmpData:BitmapData = extractPNG(bytes);
    		return bmpData;
    	}
    

    It works fine, but maybe I could do something simplier that that?


Log in to reply