2012-05-22 52 views
0

我得到錯誤的as3corelib系列化出錯

Error #2099: The loading object is not sufficiently loaded to provide this information

,當我嘗試使用的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); 
      } 
     }   
    } 

    .... 

} 

}

這裏是塊的代碼,真的沒有。

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

我建議你不要試圖序列化任何形式的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以幫助分離邏輯。

+0

好的,謝謝你的回答。去實現這一點。 – Marek

+1

工程就像一個魅力,謝謝 – Marek