I have a custom set of classes that automatically creates entities based on their Ogmo names, and uses Flash reflection to set member variables automatically based on the Ogmo entity’s attributes. It’s super helpful and only ends up requiring one line of code to set up each class.
The basic technique is something like this:
var types:Dictionary = new Dictionary();
types["MyType"] = MyType; // where MyType is an Entity class
// etc
for each (var layer:XML in ogmoXML.children())
{
for each (var entity:XML in layer.children())
{
var construct:Class = types[String(entity.name())];
if (construct)
{
add(createInstance(construct, entity));
}
}
}
static function createInstance(construct:Class, entity:XML):Entity
{
var e:Entity = new construct();
for each (var attribute:XML in entity.attributes())
{
var name:String = attribute.name();
try
{
var value:String = entity.@[name];
// most values convert correctly from strings,
// thanks to as3 type coercion
// booleans don't, so we need to cheat
e[name] = (value == "True" || value == "False")
? value == "True" : value;
}
catch (e:ReferenceError) {}
}
return e;
}
This is mostly off the top of my head and I can’t test it right at the moment, but it should get you on the right track.
Some caveats:
- This doesn’t work with grids or tilemap layers, although it shouldn’t be difficult to add support.
- There’s no way to pass arguments to constructors, so any parameters have to have a default value.
- Any properties that you want to set this way have to be public.
- Ogmo properties like scale and rotation won’t be automatically applied to graphics, so you’ll need to handle that separately. I have a base XMLEntity class with a load(entity:XML) method that I override when I need to get additional information from the node,
I hope that helps!