/// <summary> /// Creates a new animated sprite /// </summary> /// <param name="gd">GraphicsDevice the graphics device with which to use to load the texture</param> /// <param name="XMLURL">String the location to the XML document which describes this sprite</param> public cAnimatedSprite(String animationName) : base() { _curFrame = 0; _animationIncrement = 1; _texName = animationName; _animationData = cSpriteManager.Instance.getAnimatedTexture(_texName); _texture = _animationData.texture; _stopped = true; }
private void parseAnimation(cAnimatedTexture anim, GraphicsDevice gd, ContentManager cm) { String textureURL = ""; anim.animations = new List<cAnimation>(); #region XMLParsing // If the XML document isnt in the correct format that we expect then // an XmlException will be thrown to alert the user to the fact. try { // Load the XML document into the reader XmlTextReader reader = new XmlTextReader(anim.URL); int currentAnim = -1; int frameLength = 10; // Loop through all the elements while (reader.Read()) { if (reader.NodeType == XmlNodeType.Element) { String elementName = reader.Name; // If its our top-level elemennt if (elementName == "sprite") { // Get the URL for where to load the sprite reader.MoveToAttribute(0); if (reader.Name == "url") { anim.texture = cm.Load<Texture2D>(reader.Value); } } // If its an animation of the sprite if (elementName == "animation") { cAnimation a = new cAnimation(); a.frames = new List<cFrame>(); // Load all the attributes for this animation if (reader.HasAttributes) { // For all the attributed for this animation for (int i = 0; i < reader.AttributeCount; i++) { reader.MoveToAttribute(i); // Getting the name of the animation if (reader.Name == "name") { a.name = reader.Value; } // Getting its loop type if (reader.Name == "loop") { a.loopType = reader.Value; } // Getting the generic length of each frame if (reader.Name == "frameLength") { frameLength = Int32.Parse(reader.Value); } } } currentAnim = anim.animations.Count; anim.animations.Add(a); } // If its a frame of the animation if (elementName == "frame") { cAnimation a = anim.animations[currentAnim]; // Creating a new frame object cFrame curFrame = new cFrame(); // Setting the default attributed for the frame curFrame.l = frameLength; if (reader.HasAttributes) { for (int i = 0; i < reader.AttributeCount; i++) { reader.MoveToAttribute(i); // Getting the protperties of the frame if (reader.Name == "number") { curFrame.num = Int32.Parse(reader.Value); } if (reader.Name == "x") { curFrame.x = Int32.Parse(reader.Value); } if (reader.Name == "y") { curFrame.y = Int32.Parse(reader.Value); } if (reader.Name == "w") { curFrame.w = Int32.Parse(reader.Value); } if (reader.Name == "h") { curFrame.h = Int32.Parse(reader.Value); } if (reader.Name == "l") { curFrame.l = Int32.Parse(reader.Value); } } } a.frames.Add(curFrame); } } } } catch (XmlException e) { Console.WriteLine("error occured: " + e.Message); } #endregion }
public void addAnimation(String XMLURL, String name) { cAnimatedTexture tex = new cAnimatedTexture(); tex.name = name; tex.URL = XMLURL; parseAnimation(tex, Portal2D.GD, Portal2D.CM); _animatedTextures.Add(tex); }