multiple sprites for one character

January 31, 2009 by admin
Filed under: Xbox360 News 

I will try and explain this the best i can.  But basically i have a sprite which puts a fighter on my screen, and when the forward and back keys are pushed, it walks.  Now i want to make this same character jump.  This is where i load in my sprite and get the values of each specific sprite

        protected override void Initialize()  
        {  
            spriteBatch = new SpriteBatch(graphics.GraphicsDevice);  
            graphics.PreferredBackBufferWidth = GameConstants.ScreenWidth;  
            graphics.PreferredBackBufferHeight = GameConstants.ScreenHeight;  
            graphics.IsFullScreen = false;  
            graphics.ApplyChanges();  
            Window.Title = "UltimateFighter — Coursework 3";  
 
            walking.Initialize(0, 450, 6f, 130);  
            walking.Add(3, 10, 134, 212);  
            walking.Add(132, 10, 148, 212);  
            walking.Add(280, 10, 141, 212);  
            walking.Add(420, 10, 120, 212);  
            walking.Add(543, 10, 118, 212);  
            walking.Add(660, 10, 124, 212);  
 
            graphics.SynchronizeWithVerticalRetrace = false;  
            IsFixedTimeStep = false;  
 
            base.Initialize();  
            this.IsFixedTimeStep = false;   
        }  
 
        protected override void LoadContent()  
        {  
            spriteSheet = content.Load<Texture2D>("Content/sprites/kenWalk");  
            backgroundTexture = Content.Load<Texture2D>("Content/background");  
        } 

 

Now the problem is, if i now load in the jumping sprite, and find the location of each induvidual sprite, that would just put 2 characters on my screen.  One which walks and one which jumps.  This is the code i use to make it walk

#region Using Statements  
using System;  
using System.Collections;  
using System.Collections.Generic;  
using Microsoft.Xna.Framework;  
using Microsoft.Xna.Framework.Audio;  
using Microsoft.Xna.Framework.Content;  
using Microsoft.Xna.Framework.Graphics;  
using Microsoft.Xna.Framework.Input;  
using Microsoft.Xna.Framework.Storage;  
using UltimateFighter;
#endregion  
 
 
namespace Ultimate_Fighter  
{  
 
    
    class AnimatedSprite  
    {  
         
        public ArrayList sprites = new ArrayList();  
        public int counter = 0;  
        public float frameTimer = 0f;  
        protected float width;  
        protected float speed = 6;  
        protected Vector2 direction;  
        protected Vector2 origin;  
        protected Vector2 position = Vector2.Zero;  
        protected SpriteEffects effects;  
 
    
        public void Initialize(int x, int y, float Speed, float Width)  
        {  
            direction.X = -1f;  
            direction.Y = 0f;  
            origin.X = 0;  
            origin.Y = 0;  
            position.X = x;  
            position.Y = y;  
            speed = Speed;  
            width = Width;  
 
        }  
 
        float frameDistance = 0;  
        public void Update(GameTime gameTime)  
        {  
            // Get the game pad state.  
            GamePadState joyState = GamePad.GetState(PlayerIndex.One);  
            // get keyboard state  
            KeyboardState keyState = Keyboard.GetState();  
            float timeDelta = (float)gameTime.ElapsedGameTime.TotalSeconds;  
              
            if (joyState.DPad.Right == ButtonState.Pressed || keyState.IsKeyDown(Keys.Right))  
            {     
               // if (effects == SpriteEffects.None) effects = SpriteEffects.FlipHorizontally;  
                 
                if (direction.X == -1) direction.X = 1;  
                Walk();  
            }  
            if (joyState.DPad.Left == ButtonState.Pressed || keyState.IsKeyDown(Keys.Left))  
            {  
               // if (effects == SpriteEffects.FlipHorizontally) effects = SpriteEffects.None;  
                 
                if (direction.X == 1) direction.X = -1;  
                Walk();  
 
            }  
        }  
 
         
        public void Add(int x, int y, int width, int height)  
        {  
            sprites.Add(new Rectangle(x, y, width, height));  
        }  
         
        public void Walk()  
        {    
            position.X += direction.X * speed;  
            position.Y += direction.Y * speed;  
            // if sprite goes off left, keep it on screen  
            if (position.X < 0) position.X = 0;  
            // if sprite goes off right, keep it on screen  
            if (position.X > GameConstants.ScreenWidth - width)  
                position.X = GameConstants.ScreenWidth - width;  
            // increment frame distance  
            frameDistance += GameConstants.frameInc;  
            // if its over threshold  
            if (frameDistance >= GameConstants.frameThreshold)  
            { // reset to 0  
                frameDistance = 0f;  
                // increment the sprite counter  
                counter++;  
                // keep the counter in the range 0 - sprites.Count  
                counter = counter % sprites.Count;  
            }  
        }  
 
        public void Draw(SpriteBatch spriteBatch, Texture2D spriteSheet)  
        {  
            spriteBatch.Draw(spriteSheet, position, ((Rectangle)sprites[counter]), Color.White, 0f, origin, 1f,  
                    effects, 0f);  
        }  
 
    }  

Now that is what my instance is too when i do walking.Add in the first code snipplet.  For my jumping, i have created a class exactly the same as the one above, but am doing it for the up key pushed instead.  But how would i get all the sprites to be on just one character, rather than having several characters on my screen each performing a different action.  I cannot find any tutorials on this at all.  They all show how to perform one animation on one sprite.  Any advise at all would be great.  I am trying my hardest to work on this as i am now extremely addicted and cannot put it down!
cheers

Comments

Feel free to leave a comment...
and oh, if you want a pic to show with your comment, go get a gravatar!