0

我收到错误

错误 #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
    {
        ...
    }
}

任何线索为什么会这样?

4

1 回答 1

1

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

// 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);

您可以通过将对象构造/填充逻辑移动到工厂方法中来进一步扩展它,以帮助分离逻辑。

于 2012-05-22T13:37:34.903 回答