2013-02-17 2 views
1

내 프로그램의 대부분의 데이터를로드하는 xml 구조가 있습니다. 이 경우 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) -class-from-as3-just-knowing-his-name) – bobobobo

답변

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

관련 문제