Exemplo n.º 1
0
        /// <summary>
        /// Plays a given animation. If the animation is already playing it returns doing nothing.
        /// If there is no animation by that name it does nothing.
        /// </summary>
        /// <param name="animationName">the name of the animation to play</param>                
        public void play(String animationName)
        {
            for (int i = 0; i < _animationData.animations.Count; i++)
            {
                cAnimation a = _animationData.animations[i];

                if (a.name == animationName)
                {
                    if (_currentAnimation != null)
                    {
                        if (_currentAnimation.Equals(a)) { return; } // if we are already playing this animation dont do anything
                    }

                    // Else we need to reset the variables
                    _currentAnimation = a;
                    _time = 0;
                    _currentFrame = _currentAnimation.frames[0];
                    _curFrame = 0;
                    _stopped = false;
                }
            }
        }
Exemplo n.º 2
0
        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
        }