as3corelib序列化出错了

时间:2012-05-22 13:19:58

标签: json actionscript-3 flash

我收到错误

  

错误#2099:加载对象未充分加载以提供此信息

当我尝试使用as3corelib将对象编码为JSON时。 我成功编写了一些没有父或子的值对象,所以我知道库工作,这个问题可能与addChild或类似的东西有关。这只是猜测。

董事会被添加到这样的阶段:

stage.addChild(board);

当我没有将电路板添加到舞台并尝试序列化时,我得到了不同的错误:

undefined
at XML/http://adobe.com/AS3/2006/builtin::copy()
at global/describeTraits()
at global/avmplus::describeType()
at global/flash.utils::describeType()
    ...

董事会成员:

public class Board extends Sprite
{
    public var board:Array;
    public var blockColor:uint = 0xE3E3E3;
    public var blockLength:uint

    public function Board(blockLength:uint)
    {
        super();
        x = 0;
        y = 0;
        this.blockLength = blockLength;
        //buttonMode = true;
        // Setting up two dim array
        board = new Array(10);
        for (var k:int = 0; k < board.length; k++) 
        {
            board[k] = new Array(10);
        }


        for (var i:int = 0; i < 10; ++i) 
        { 
            for(var j:int = 0; j < 10; ++j)
            {
                var block:Block = new Block(i*blockLength, j*blockLength);  
                board[i][j] = block;
                this.addChild(block); // here I add children
                block.drawBlock(blockLength, blockColor);
                block.addEventListener(MouseEvent.CLICK, blockClicked);
            }
        }           
    }

    ....

}

}

这是Block的代码,实际上没什么。

public class Block extends Sprite
{

    public var cos:int = 5; // test

    public function Block(x:uint, y:uint)
    {
        ...
    }

    public function drawBlock(length:uint, color:uint):void
    {
        ...
    }
}

任何线索为什么会这样?

1 个答案:

答案 0 :(得分:1)

我建议您不要尝试序列化任何形式的DisplayObject;相反,您应该只是序列化View使用的基础数据(属性);很难从上面的代码中给出一个确切的答案,但请考虑以下内容:

// Simple Model object which represents the BlockView's underlying data.
public class BlockViewModel {
    public var x : Number;
    public var y : Number;
}

// Renders the BlockViewModel on screen.
public class BlockView extends Sprite {
    public var position : BlockViewModel;

    // Constructor requires a BlockViewModel object.
    public function BlockView(position : BlockViewModel) {
        this.position = position;
        draw();
        reposition();
    }

    private function draw() : void {
        // Omitted...
    }

    // Update the block's position based on the model.
    private function reposition() : void {
        this.x = this.position.x;
        this.y = this.position.y;
    }

    // Setter for the block's current position.
    public function setX(value : Number) : void {
        this.position.x = value;
        reposition();
    }
}

通过上面的示例,您只需要在保存状态时序列化BlockViewModel对象,例如:

var serailizedBlockData : String = JSON.encode(blockView.position);

然后,您可以通过反序列化数据重新创建新的BlockView:

// Convert from JSON -> Object.
var blockData : Object = JSON.decode(serializedBlockData);

// Create and populate a BlockViewModel with the deserialized data.
var position : BlockViewModel = new BlockViewModel();
position.x = blockData.x;
position.y = blockData.y;

// Create a new view using the serialized data.
var blockView = new BlockView(position);

您可以通过将对象构造/填充逻辑移动到Factory method来帮助分离逻辑来进一步扩展它。