Mhmm, so far so good. The problem is that you still have to reference the animation by name(albeit a nice, typesafe constant) rather than by index. So whatever other place you reference it from needs to have SpritemapCatalog.CAT_WALK
hard coded into it, rather than learning about it dynamically at runtime.
Heres what I ended up doing to solve it:
public class SpriteMapWrapper
{
public var spriteMap:Spritemap;
public var animations:Vector.<String>;
public function SpriteMapWrapper(spriteMap:Spritemap, animations:Vector.<String>)
{
this.spriteMap = spriteMap;
this.animations = animations;
}
}
so the client can call
myWrapper.spriteMap.play(myWrapper.animations[0]);
the client can be otherwise completely unaware of any details of the spritemap. It just has to be aware that there is a spritemap and it has animations. That means any client that has the wrapper can call the animations without having any concrete animation names coded into them.
does that make sense?
The upshot of this is that I can now dynamically bind entities to new visual layers with one line of code like this. The following code uses a factory which holds a number of “createX” classes similar to the one Zach made for a spritemap
var thing:entity = new entity(/*middle of the screen*/)
FP.world.add(thing);
thing.addvisual(new visual(referenceToThing, createSpriteMapWrapper("swordguy"), xofThing + 10, yofThing + 10);
thing.addvisual(new visual(referenceToThing, createSpriteMapWrapper("swordguy"), xofThing - 10, yofThing - 10);
thing.addvisual(new visual(referenceToThing, createSpriteMapWrapper("swordguy"), xofThing, yofThing+20);
thing.addvisual(new visual(referenceToThing, createSpriteMapWrapper("swordguy"), xofThing, yofThing-20);
thing.addvisual(new visual(referenceToThing, createSpriteMapWrapper("swordguy"), xofThing+20, yofThing);
and what I just did was create a single entity on screen that is parented to five swordguy graphics that all appropriately use their walking animations or sword swinging animations as needed (the helperObjects manage their own state and decide when to play which animation)
This would be great for a little real time strategy where one entity needs multiple independantly functioning graphics but they all need to share common states and behaviors. Or, picture this (a fight planned for my game):
a boss fight with a giant robot and a team of evil engineers in a junkyard. While the robot is attacking, the engineers are running around the junkyard, picking up salvage and attaching it to the robot. the robot incorporates the salvage and correspondingly gains new movements and attack patterns.
the engineers can attach a laser beam by calling
robot.addAction(new shootLaser());
robot.addVisual(new garbageLaservisual());
and bam its ready to role.
and the really crazy part is I’ll never need to create a “giantrobot” class that subclasses entity. It will just be a regular old entity that gets composed with other helperObjects that define its in game look and feel.
The hope is that in the long run, that means extensive reusability, and incredibly simple extension.
It may or may not work, but that’s the plan.