假定您的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();
}
}
}
}
}
可能重複[我可以從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