mardi 22 novembre 2016

Saving objects with writeObject: What objects can be saved using this method?

I'm working making my first foray into the exciting world of byteArrays!

End Goal

I want to save the positions and other properties of each game element (in this case, blocks in a breakout clone) as part of a level design feature for my app, and also to more easily design levels for the game.

Current Approach

Convert data from a Vector of custom class instances (the bricks) into a ByteArray and save that data to a text file. It seems like this is working fine up to this point (can't be sure until I successfully extract that data back into a Vector object, because the text file that gets saved is pure gobbledygook).

I load a level design by reading the text file into a byteArray and then doing writeObject() into a Vector with the intention of now having a vector that contains all the bricks (this is not working).

The Problem

When I try to run my load function, the file loads, and the byteArray gets "filled" with data, but when I try to do writeObject, I get all these errors(one copy of the following errors for each brick in the vector).

TypeError: Error #1034: Type Coercion failed: cannot convert Object@92cdcb9 to flash.geom.Point.
TypeError: Error #1034: Type Coercion failed: cannot convert Object@92cde09 to flash.geom.Point.
TypeError: Error #1034: Type Coercion failed: cannot convert Object@92df041 to flash.geom.ColorTransform.
TypeError: Error #1034: Type Coercion failed: cannot convert Object@92df161 to flash.geom.Point.
TypeError: Error #1034: Type Coercion failed: cannot convert Object@92df281 to flash.geom.Point.
TypeError: Error #1034: Type Coercion failed: cannot convert Object@92df431 to
TypeError: Error #2004: One of the parameters is invalid.

My custom brick class is an extension of the Sprite class. But it additionally has properties that depend on Point and ColorTransform objects. Oddly, nowhere in my custom class do I have any reference to or use of SoundTransform... so that error seems glaringly odd. I'll post my custom class if anyone wants to look at it.

My Save and Load Methods

    private function saveLevelDesign(brVec:Vector.<LineTestBlock>):void{
        var file:File = File.documentsDirectory; 
        file = file.resolvePath("AnimationFiles/brickLevels/lev_001.txt");,FileMode.WRITE);
        var bytes:ByteArray = new ByteArray();
        bytes = brickArrayToByteArray(brVec);

    private function loadLevelDesign():void{
        var file:File = File.documentsDirectory;
        file = file.resolvePath("AnimationFiles/brickLevels/lev_001.txt");,FileMode.READ);
        file.addEventListener(Event.COMPLETE, loaded); 

    private function ioError(ioE:IOErrorEvent):void{

    private function loaded(e:Event):void{
        var bytes:ByteArray = new ByteArray();
        trace(bytes.length,"bytes length"); // 0 bytes length
        var vec:Vector.<LineTestBlock> = new Vector.<LineTestBlock>;
        for (var i:int = 4; i < _playerTurn._brickArray.length; i++){
        trace(bytes.length,"bytes length"); // 53516 bytes length

        destroyBricks(_playerTurn); // just removes all bricks on the stage 

        vec = byteArrayToBrickArray(bytes); // calling this function throws all those errors
        trace("vector length:",vec.length); // vector length 208 (this is the correct number of bricks, so that's good)

Anyone see if I doing something wrong, or not understanding something?

Aucun commentaire:

Enregistrer un commentaire