Platformer.AnimationPlayer.Draw C# (CSharp) Method

Draw() public method

Advances the time position and draws the current frame of the animation.
public Draw ( GameTime gameTime, SpriteBatch spriteBatch, Vector2 position, SpriteEffects spriteEffects ) : void
gameTime Microsoft.Xna.Framework.GameTime
spriteBatch Microsoft.Xna.Framework.Graphics.SpriteBatch
position Vector2
spriteEffects SpriteEffects
return void
        public void Draw(GameTime gameTime, SpriteBatch spriteBatch, Vector2 position, SpriteEffects spriteEffects)
        {
            if (Animation == null)
                throw new NotSupportedException("No animation is currently playing.");

            // Process passing time.
            time += (float)gameTime.ElapsedGameTime.TotalSeconds;
            while (time > Animation.FrameTime)
            {
                time -= Animation.FrameTime;

                // Advance the frame index; looping or clamping as appropriate.
                if (!paused)
                {
                    if (Animation.IsLooping)
                    {
                        frameIndex = (frameIndex + 1) % Animation.FrameCount;
                    }
                    else
                    {
                        frameIndex = Math.Min(frameIndex + 1, Animation.FrameCount - 1);

                        if (!done && frameIndex == Animation.FrameCount - 1)
                        {
                            Animation.Done();
                            done = true;
                        }
                    }
                }
            }

            // Calculate the source rectangle of the current frame.
            Rectangle source = new Rectangle(FrameIndex * Animation.Texture.Height, 0, Animation.Texture.Height, Animation.Texture.Height);

            // Draw the current frame.
            spriteBatch.Draw(Animation.Texture, position, source, Color.White, 0.0f, Origin, 1.0f, spriteEffects, 0.0f);
        }

Usage Example

Example #1
0
        /// <summary>
        /// Draws the animated enemy.
        /// </summary>
        public void Draw(GameTime gameTime, SpriteBatch spriteBatch)
        {
            // Stop running when the game is paused, before turning around, or if the possessing player is idle.
            if (!Level.Player.IsAlive ||
                Level.ReachedExit ||
                Level.TimeRemaining == TimeSpan.Zero ||
                waitTime > 0 || this.direction == FaceDirection.Idle)
            {
                if (isNearest || possessed)
                {
                    sprite.PlayAnimation(evilIdleAnimation);
                    spriteGlow.PlayAnimation(evilIdleAnimationGlow);
                }
                else
                {
                    sprite.PlayAnimation(idleAnimation);
                }
            }
            else
            {
                if (isNearest || possessed)
                {
                    sprite.PlayAnimation(evilRunAnimation);
                    spriteGlow.PlayAnimation(evilRunAnimationGlow);
                }
                else
                {
                    sprite.PlayAnimation(runAnimation);
                }
            }

            if (!player.IsOnGround && monsterType == MonsterType.Lantern)
            {
                sprite.PlayAnimation(evilJumpAnimation);
            }

            // Draw facing the way the enemy is moving.
            SpriteEffects flip = direction > 0 ? SpriteEffects.FlipHorizontally : SpriteEffects.None;

            if (isNearest)
            {
                spriteGlow.Draw(gameTime, spriteBatch, Position, flip);
                sprite.Draw(gameTime, spriteBatch, Position, flip);
            }
            else
            {
                sprite.Draw(gameTime, spriteBatch, Position, flip);
            }
        }
All Usage Examples Of Platformer.AnimationPlayer::Draw