2013-02-17 67 views
1

我有xml-structure,其中載入了我的程序的大部分數據。在這種情況下,我想實例化一個在xml中指定的類。我想我可以在xml中編寫類名,然後實例化它並將參數傳遞給它。原來並不那麼容易!在As3中創建具有類'name'字符串的實例

我想這樣的代碼:

  //special objects 
     for each (o in xml.Objects[0].special) 
     { 
      p.x = [email protected]; 
      p.y = [email protected]; 
      s.x = [email protected]; 
      s.y = [email protected]; 
      trace([email protected]); 
      //var type:Class = [email protected] as Class; 
      var type:Class = getDefinitionByName(String([email protected])) as Class; 

      trace(type); 
      objectArray.push(new type(p, s)); 
      trace("special"); 
     } 

正如你可以看到我有我在XML文件類名屬性類的名稱。我設法與getDefinitionByName定義(至少一個跟蹤顯示正確的類名),但是當我嘗試實例化,並將其推入陣列我得到它開始

錯誤#2136錯誤的堆:SWF文件file:///Users/tuomas/Dropbox/Flash/ScorpionBox/bin-debug/ScorpionBox.swf包含無效數據。

任何想法,我應該如何與此?

+0

可能重複[我可以從AS3創建一個類的實例,只知道他的名字?](http://stackoverflow.com/questions/7597343/can-i-create-an-instance-of-a -s3-just-known-his-name) – bobobobo 2014-09-30 15:00:10

回答

4

假定您的XML類型定義由getQualifiedClassName()獲得的限定名稱組成,請實例化該類型並根據需要應用屬性。顯示對象的

實施例的實例化由XML定義:

package 
{ 
    import flash.display.DisplayObject; 
    import flash.display.Sprite; 
    import flash.utils.getDefinitionByName; 

    public class XmlParser extends Sprite 
    { 
     public var xml:XML = <objects> 
           <object type="flash.display::Sprite" x="0" y="0" width="100" height="100" /> 
           <object type="flash.display::MovieClip" x="0" y="0" width="100" height="100" /> 
          </objects>; 

     public function XmlParser() 
     { 
      for each (var object:XML in xml.children()) 
      { 
       var type:Class = getDefinitionByName([email protected]) as Class; 
       var instance:DisplayObject = new type(); 
       instance.x = [email protected]; 
       instance.y = [email protected]; 
       instance.width = [email protected]; 
       instance.height = [email protected]; 

       addChild(instance); 
      } 
     } 
    } 
} 

你可以還的describeType對象時序列化到XML,然後通過遍歷XML的屬性,諸如應用性能回對象實例:

package 
{ 
    import flash.display.DisplayObject; 
    import flash.display.Sprite; 
    import flash.utils.getDefinitionByName; 

    public class XmlParser extends Sprite 
    { 
     public var xml:XML = <objects> 
           <object type="flash.display::Sprite" x="0" y="0" width="100" height="100" /> 
           <object type="flash.display::MovieClip" x="0" y="0" width="100" height="100" /> 
          </objects>; 

     public function XmlParser() 
     { 
      for each (var object:XML in xml.children()) 
      { 
       var type:Class = getDefinitionByName([email protected]) as Class; 
       var instance:DisplayObject = new type(); 
       addChild(instance); 

       for each (var attribute:XML in [email protected]*) 
       { 
        if(attribute.name() == "type") { continue; } 

        trace("setting: " + attribute.name() + " = " + attribute.toXMLString()); 
        instance[attribute.name().toString()] = attribute.toXMLString(); 
       } 
      } 
     } 
    } 
} 
+0

非常感謝!我的問題是我沒有在字符串中加入「::」,直到這篇文章才發現它。它現在就像一種魅力:) – Tumetsu 2013-02-17 21:56:13

相關問題