2 Replies - 12699 Views - Last Post: 18 November 2010 - 07:54 PM

#1 lerning  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 50
  • Joined: 08-August 09

Gametime & ElapsedTime

Posted 16 November 2010 - 10:53 PM

I'm sending this from my iPhone so forgive me if my spelling sucks. I have been reading on sprite animation with frames, but I don't understand how the elapsed time works or what it's purpose is in the frame animation, could someone break it down for me even lower, I had seen a tutorial written here by kilorn and another tutorial that had to do with animating feilong from street fighter. Maybe an over commented tutorial could help, everytime I see the elapse time class I'm always wondering what value it holds that makes the frame change, I really don't know, help on this would be greatly appreciated.

This post has been edited by lerning: 16 November 2010 - 10:54 PM


Is This A Good Question/Topic? 1
  • +

Replies To: Gametime & ElapsedTime

#2 lerning  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 50
  • Joined: 08-August 09

Re: Gametime & ElapsedTime

Posted 17 November 2010 - 05:48 PM

So I found an excerpt from a book on how the author said it should be priority to use elapsed time for moving a sprite. In fact this is a sample piece from the book, related to 4.0 XNA:

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace Understanding_ElapsedTime
{
    
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        Texture2D spriteTexture;
        Vector2 spritePosition;
        float spriteSpeed = 200f;

        int dispw, disph;


        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
            dispw = graphics.PreferredBackBufferWidth = 400;
            disph = graphics.PreferredBackBufferHeight = 400;
        }

        
        protected override void Initialize()
        {
            // TODO: Add your initialization logic here
            this.IsMouseVisible = true;
            base.Initialize();
        }

        
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);
            spriteTexture = this.Content.Load<Texture2D>("startFrame");
            spritePosition = new Vector2(dispw / 3, disph / 2);
            // TODO: use this.Content to load your game content here
        }

       
        protected override void UnloadContent()
        {
            spriteBatch.Dispose();
            // TODO: Unload any non ContentManager content here
        }

        
        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();

            KeyboardState currentKeyboardState = Keyboard.GetState();

            
            if (currentKeyboardState.IsKeyDown(Keys.Right))
            spritePosition.X += spriteSpeed * (float)gameTime.ElapsedGameTime.TotalSeconds;

            if (currentKeyboardState.IsKeyDown(Keys.Left))
            spritePosition.X -= spriteSpeed * (float)gameTime.ElapsedGameTime.TotalSeconds;

/*            if (spritePosition.X > graphics.PreferredBackBufferWidth)
                spritePosition.X = graphics.PreferredBackBufferWidth;

            if (spritePosition.X < graphics.PreferredBackBufferWidth)
                spritePosition.X = graphics.PreferredBackBufferWidth;
            */
            // TODO: Add your update logic here

            base.Update(gameTime);
        }

       
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);
            spriteBatch.Begin();
            // TODO: Add your drawing code here
            spriteBatch.Draw(spriteTexture, spritePosition, Color.White);
            spriteBatch.End();
            base.Draw(gameTime);
        }
    }
}




I understand how its smoother when animating movement.(Like hwne a key is pressed for "1" second the sprites position will move 200px but I still dont quite get how its used in animating frames on sprite sheets. I also understand the quality over using FPS, but im just muddled and wouldnt know where to start with using it to change the frames of a sprite. I saw the tutorials but again like I said, I dont know how its logic works, any help would be greatly appreciated.

This post has been edited by lerning: 17 November 2010 - 05:51 PM

Was This Post Helpful? 0
  • +
  • -

#3 SixOfEleven  Icon User is offline

  • using Caffeine;
  • member icon

Reputation: 945
  • View blog
  • Posts: 6,342
  • Joined: 18-October 08

Re: Gametime & ElapsedTime

Posted 18 November 2010 - 07:54 PM

The parameter gameTime measures the time since the last call to the Update method. The ElapsedGameTime is measured in terms of game time. It is a rather reliable way of measuring the time between frames and it is pretty constant. Therefore when you do something as follows:

            spritePosition.X -= spriteSpeed * (float)gameTime.ElapsedGameTime.TotalSeconds;



You can assure that the sprite is moving at a constant speed on all computers. But that doesn't really answer your question on how it works when animating sprites with sprite sheets. You know that in 1 second there are 1000 milliseconds. You can keep track, in a variable, the elapsed time between frames. When that value is greater than a constant, you move onto the next frame and reset the variable back to zero.

So, basically you take the number of frames you want to animate per second and you have another that measures the time since the frame changed.

float frameRate = 1f / framesPerSecond;
float frameTimer = 0f;



Now, in each pass through you use the ElapsedGameTime property to increase the frame timer. You compare that to the frameRate. If the frame timer is greater than the frame rate you move to the next frame.

frameTimer += gameTime.ElapsedGameTime.TotalMillisecond;

if (frameTimer > frameRate)
{
    frameTimer = 0f;
    // move to the next frame
}



I hope that helps a little.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1