4 Replies - 1653 Views - Last Post: 07 March 2016 - 05:18 PM

#1 FLEUGZERBERWERBERGARKTEN  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 04-January 16

XNA Sprite Animation Help

Posted 04 January 2016 - 12:39 AM

Hey all, I'm really new to XNA and C# as a whole. I've been following some random tutorials on YouTube following sprite loading, animation and all that jazz. I've managed to make a game with sprites that animate, but now when I try to set things for the animated sprites in my Game1 class I don't have anything to use. I want the animated sprite to have things like left and right and other things like that so I can do stuff with them. I'd like to know what I need to do or what I'm doing wrong thanks. I'll paste my code in and I'm really sorry if this is a really stupid thing to ask, cause I'm stumped.
Game1.cs
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;
using ScrollingBackgroundSpace;

namespace Korean_Dogfight_II
{
    
    public class Game1 : Microsoft.Xna.Framework.Game
    {

        int screenWidth;
        int screenHeight;

        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;

        Animation player;

        Enemy b29;

        public Texture2D background;

        bool movingLeft =  true;

        
        
        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
        }

        protected override void Initialize()
        {

            graphics.PreferredBackBufferHeight = 800;
            graphics.PreferredBackBufferWidth = 800;
            graphics.IsFullScreen = false;
            graphics.ApplyChanges();
            window.Title = "Korean Dogfight";

            bullets = new Bullets[10];

           

            base.Initialize();
        }

        
        private ScrollingBackground myBackground;

        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.

            spriteBatch = new SpriteBatch(GraphicsDevice);
            myBackground = new ScrollingBackground();
            Texture2D background = Content.Load<Texture2D>("sky1");
            myBackground.Load(GraphicsDevice, background);


            player = new Animation(Content.Load<Texture2D>("mig21"), new Vector2(graphics.GraphicsDevice.Viewport.Width / 2, 625), 123, 59);
            
            b29 = new Enemy(Content.Load<Texture2D>("b-29"), new Vector2(graphics.GraphicsDevice.Viewport.Width / 2, 150), 119, 185);
            
            screenWidth = GraphicsDevice.PresentationParameters.BackBufferWidth;
            screenHeight = GraphicsDevice.PresentationParameters.BackBufferHeight;

        }

        protected override void UnloadContent()
        {
          
        }

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

            float elapsed = (float)gameTime.ElapsedGameTime.TotalMilliseconds;


            // MIG-21
            player.Update(gameTime);
            
            
            

            // B29
            b29.Update(gameTime);


            //if (movingLeft)
            //{
            //    b29.AnimateLeft(gameTime);
            //}
            //else if (!movingLeft)
            //{
            //    b29.AnimateRight(gameTime);
            //}
            //if (b29 >= 0 && movingLeft) movingLeft = false;

            //if (b29 >= (graphics.GraphicsDevice.Viewport.Width - b29) && !movingLeft) movingLeft = true;

           

            // TODO: Add your update logic here

                            myBackground.Update(elapsed * 50);
                          
            base.Update(gameTime);
        }

        
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.SkyBlue);

            spriteBatch.Begin();

            DrawBackground();
            myBackground.Draw(spriteBatch);

            spriteBatch.End();

            base.Draw(gameTime);


            // TODO: Add your drawing code here

            base.Draw(gameTime);

            spriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend);

            
            player.Draw(spriteBatch);
            b29.Draw(spriteBatch);
            //spriteBatch.Draw(B29, B29Pos, Color.White);

            spriteBatch.End();
        }

        
        private void DrawBackground()
        {
            Rectangle screenRectangle = new Rectangle(0, 0, screenWidth, screenHeight);
            
        }
        
    }
}
 


The code bits I've commented out are parts that aren't working so I disabled them for a bit to try and figure out what was going wrong.

Animation.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework;



namespace Korean_Dogfight_II
{
    public class Animation
    {
        Texture2D texture;
        Rectangle rectangle;
        Vector2 position;
        Vector2 origin;
        Vector2 velocity;

        int currentFrame;
        int frameHeight;
        int frameWidth;
        

        float timer;
        float interval = 75;

        public Animation(Texture2D newTexture, Vector2 newPosition, int newFrameHeight, int newFrameWidth)
        {
            texture = newTexture;
            position = newPosition;
            frameHeight = newFrameHeight;
            frameWidth = newFrameWidth;
            

        }
        public void Update(GameTime gameTime)
        {
            rectangle = new Rectangle(currentFrame * frameWidth, 0, frameWidth, frameHeight);
            origin = new Vector2(rectangle.Width / 2, rectangle.Height / 2);
            position = position + velocity;

            if (Keyboard.GetState().IsKeyDown(Keys.Right))
            {
                AnimateRight(gameTime);
                velocity.X = 3;
            }
            else if (Keyboard.GetState().IsKeyDown(Keys.Left))
            {
                AnimateLeft(gameTime);
                    velocity.X = -3;
            }
            else
            {
                NotMoving(gameTime);
                    velocity.X = 0;
            }



        }
        public void AnimateRight(GameTime gameTime)
        {
            timer += (float)gameTime.ElapsedGameTime.TotalMilliseconds / 2;
            if (timer > interval)
            {
                currentFrame++;
                timer = 0;
            }
                if (currentFrame < 7 || currentFrame > 11)
                currentFrame = 8;
        }

        public void AnimateLeft(GameTime gameTime)
        {
            timer += (float)gameTime.ElapsedGameTime.TotalMilliseconds / 2;
            if (timer > interval)
            {
                currentFrame++;
                timer = 0;
            }
            if (currentFrame > 3)
                currentFrame = 0;
        }

        public void NotMoving(GameTime gameTime)
        {
            timer += (float)gameTime.ElapsedGameTime.TotalMilliseconds / 2;
            if (timer > interval)
            {
                currentFrame++;
                timer = 0;
            }
            if (currentFrame < 3 || currentFrame > 7)
                currentFrame = 4;
        }
        public void Draw(SpriteBatch spriteBatch)
        {
            spriteBatch.Draw(texture, position, rectangle, Color.White, 0f, origin, 1.0f, SpriteEffects.None, 0f);
        }
    }
}




Enemy.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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 Korean_Dogfight_II
{
    public class Enemy
    {

        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;

        Boolean movingLeft;

        Texture2D texture;
        Rectangle rectangle;
        Vector2 position;
        Vector2 origin;
        Vector2 velocity;

        int currentFrame;
        int frameHeight;
        int frameWidth;

        float timer;
        float interval = 300;

        public Enemy(Texture2D newTexture, Vector2 newPosition, int newFrameHeight, int newFrameWidth)
        {
            texture = newTexture;
            position = newPosition;
            frameHeight = newFrameHeight;
            frameWidth = newFrameWidth;



        }
        public void Update(GameTime gameTime)
        {
            rectangle = new Rectangle(currentFrame * frameWidth, 0, frameWidth, frameHeight);
            origin = new Vector2(rectangle.Width / 2, rectangle.Height / 2);
            position = position + velocity;
            if (rectangle.Left >= 0)
            {
                movingLeft = false;
            }
            else if (rectangle.Left >= -graphics.GraphicsDevice.Viewport.X)
            {
                movingLeft = true;
            }
            if (movingLeft == false)
            {
                AnimateRight(gameTime);
                velocity.X = 3;
            }
            else if (movingLeft == true)
            {
                AnimateLeft(gameTime);
                velocity.X = -3;
            }
            else
            {
                NotMoving(gameTime);
                velocity.X = 0;
            }



        }
        public void AnimateRight(GameTime gameTime)
        {
            timer += (float)gameTime.ElapsedGameTime.TotalMilliseconds / 2;
            if (timer > interval)
            {
                currentFrame++;
                timer = 0;
            }
            if (currentFrame < 7 || currentFrame > 11)
                currentFrame = 8;
        }

        public void AnimateLeft(GameTime gameTime)
        {
            timer += (float)gameTime.ElapsedGameTime.TotalMilliseconds / 2;
            if (timer > interval)
            {
                currentFrame++;
                timer = 0;
            }
            if (currentFrame > 3)
                currentFrame = 0;
        }

        public void NotMoving(GameTime gameTime)
        {
            timer += (float)gameTime.ElapsedGameTime.TotalMilliseconds / 2;
            if (timer > interval)
            {
                currentFrame++;
                timer = 0;
            }
            if (currentFrame < 3 || currentFrame > 7)
                currentFrame = 4;
        }
        public void Draw(SpriteBatch spriteBatch)
        {
            spriteBatch.Draw(texture, position, rectangle, Color.White, 0f, origin, 1.0f, SpriteEffects.None, 0f);
        }
    }
}




Is This A Good Question/Topic? 0
  • +

Replies To: XNA Sprite Animation Help

#2 NeoTifa  Icon User is offline

  • NeoTifa Codebreaker, the Scourge of Devtester
  • member icon





Reputation: 4055
  • View blog
  • Posts: 18,098
  • Joined: 24-September 08

Re: XNA Sprite Animation Help

Posted 05 January 2016 - 09:57 AM

Have an abstract class Sprite that entities can extend from with enum Direction or something of that sort.
Was This Post Helpful? 0
  • +
  • -

#3 FLEUGZERBERWERBERGARKTEN  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 04-January 16

Re: XNA Sprite Animation Help

Posted 05 January 2016 - 08:17 PM

Okay thanks I'll try and do that.
Was This Post Helpful? 0
  • +
  • -

#4 FLEUGZERBERWERBERGARKTEN  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 04-January 16

Re: XNA Sprite Animation Help

Posted 04 March 2016 - 06:46 PM

I managed to figure out the problem with it all. With the Enemy.cs class I am trying to make it bounce off the walls and animate at the same time, but bouncing off one wall seems to collide with the code of bouncing off the other wall. I have tried some stuff and its probably a simple math error but I can't figure anything out. I'll paste the code that is bothering me in.


ENEMY.CS
public void Update(GameTime gameTime)
        {
            rectangle = new Rectangle(currentFrame * frameWidth, 0, frameWidth, frameHeight);
            origin = new Vector2(rectangle.Width, rectangle.Height);
            position = position + velocity;

            if (position.X - frameWidth <= 0)
            {
                movingRight = true;
                movingLeft = false;
                velocity.X = 3;

            }
            if (position.X >= GlobalGDClass.RightScreen)
            {
                movingLeft = true;
                movingRight = false;
                velocity.X = -3;
            }

                if (movingRight == true)
                {
                    AnimateRight(gameTime);
                }

                if (movingLeft == true)
                {
                    AnimateLeft(gameTime);
                }
        }



The part that makes it animate left or right depending on its position is the bit that gets me. If the position goes below 0 I want it to start going towards the other side of the screen, but then it conflicts with the code that makes it want to go left when it hits the right side. I am a bit stuck still but I managed to figure this out.
Was This Post Helpful? 0
  • +
  • -

#5 basteagui  Icon User is offline

  • D.I.C Head

Reputation: 14
  • View blog
  • Posts: 60
  • Joined: 08-September 13

Re: XNA Sprite Animation Help

Posted 07 March 2016 - 05:18 PM

draw a string on your screen that tells you the value of movingleft and movingright booleans.
that way you will know wether it is changing in the way you expect.


i notice you're substracting the framewidth for the left side, but you aren't adding the framewidth for the right side...
is your sprite origin in the middle of the image?

{
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1