XNA - Some animations blinking others work fine

Started by
0 comments, last by flashinpan 15 years, 5 months ago
I have a problem with how my spritesheets / animation is working. All my animations are 32 pixels high by some multiple of 32 pixels wide. The animations with more cells are working fine. But when grouped with animations with fewer animation cells, say, 3 cells, are not working right. They run through their animation sequence and then they ... BLINK .... I want to stop them from blinking...but I need some help.

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
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 Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace robotz
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class robotz : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;

        private static Song GeneratorExplode;

        int SpriteWidth = 32;
        int SpriteHeight = 32;

        float Timer = 0;

        private const string CONTENT_PATH = "..\\..\\..\\Content";
        private const string GRAPHICS_PATH = "\\img";
        private const string PATH_TO_GAMEBOARD_IMG = CONTENT_PATH + GRAPHICS_PATH + "\\gameboard.png";
        private const string PATH_TO_MEDIC_IMG = CONTENT_PATH + GRAPHICS_PATH + "\\medic.png";
        private const string PATH_TO_ENGINEER_IMG = CONTENT_PATH + GRAPHICS_PATH + "\\engineer.png";
        private const string PATH_TO_DESTROYER_IMG = CONTENT_PATH + GRAPHICS_PATH + "\\destroyer.png";
        private const string PATH_TO_WALL_IMG = CONTENT_PATH + GRAPHICS_PATH + "\\wall.png";
        private const string PATH_TO_POWERSOURCE_IMG = CONTENT_PATH + GRAPHICS_PATH + "\\powersource.png";
                    
        Texture2D gameboard;
        Texture2D powersource;

        // Set the coordinates to draw the sprite at.
        Vector2 gameboardPosition = Vector2.Zero;
        Vector2 powersourcePosition = Vector2.Zero;

        Collection<RobotzSprite> robotzSprites = new Collection<RobotzSprite>();

        public robotz()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
            graphics.IsFullScreen = true;
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            // TODO: Add your initialization logic here

            base.Initialize();
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        /// 
        // This is a texture we can render.



        protected override void LoadContent()
        {

            GeneratorExplode = Content.Load<Song>("Generator Explodes");
                        
            spriteBatch = new SpriteBatch(GraphicsDevice);

            gameboard = Texture2D.FromFile(GraphicsDevice, PATH_TO_GAMEBOARD_IMG);
            
            //for (int i = 0; i < 6; i++)
            //{
            //    Medic m = new Medic(PATH_TO_MEDIC_IMG, GraphicsDevice);
            //    robotzSprites.Add(m);
            //}
            
            //for (int i = 0; i < 2; i++)
            //{
            //    PowerSource p = new PowerSource(PATH_TO_POWERSOURCE_IMG, GraphicsDevice);
            //    robotzSprites.Add(p);
            //}

            
            //for (int i = 0; i < 8; i++)
            //{
            //    Wall w = new Wall(PATH_TO_WALL_IMG, GraphicsDevice);
            //    robotzSprites.Add(w);
            //}



            for (int i = 0; i < 1; i++)
            {
                Engineer e = new Engineer(PATH_TO_ENGINEER_IMG, GraphicsDevice);
                robotzSprites.Add(e);
            }

            //for (int i = 0; i < 8; i++)
            //{
            //    Destroyer d = new Destroyer(PATH_TO_DESTROYER_IMG, GraphicsDevice);
            //    robotzSprites.Add(d);
            //}

         
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        /// 


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

            Keys[] k = Keyboard.GetState(PlayerIndex.One).GetPressedKeys();

            if (k.Length > 0)
            {
                if (k[0] == Keys.Escape)
                {
                    this.Exit();
                }

                if (k[0] == Keys.P)
                {                  
                    MediaPlayer.Play(GeneratorExplode);
                }

            }

            MouseState ms = Mouse.GetState();
            ButtonState bs = ms.LeftButton;

            //if (bs == ButtonState.Pressed)
            //{
            //    powersourcePosition.X = ms.X;
            //    powersourcePosition.Y = ms.Y;
            //}

            int counter = 1;

            foreach (RobotzSprite rs in robotzSprites)
            {
                rs.Timer += (float)gameTime.ElapsedGameTime.TotalMilliseconds;
                rs.SpriteSourceRect = new Rectangle(rs.CurrentFrame * SpriteWidth, 0, SpriteWidth, SpriteHeight);
                rs.SpriteDestRect = new Rectangle(Convert.ToInt32(counter * 50), Convert.ToInt32(50 * (counter % 10)), 32, 32);
             
                counter++;
            }
                           
            base.Update(gameTime);
        }

        void UpdateSprite(GameTime gameTime)
        {

        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        /// 
        protected override void Draw(GameTime gameTime)
        {
            graphics.GraphicsDevice.Clear(Color.CornflowerBlue);

            // Draw the sprite.
            spriteBatch.Begin(SpriteBlendMode.AlphaBlend);
            spriteBatch.Draw(gameboard, gameboardPosition, Color.White);
            
            foreach (RobotzSprite rs in robotzSprites)
            {
                spriteBatch.Draw(rs.SpriteTexture, rs.SpriteDestRect, rs.SpriteSourceRect, Color.White);
            }            
            
            spriteBatch.End();

            base.Draw(gameTime);
        }
    }




    public class RobotzSprite
    {
        public RobotzSprite(string tempPathToImg, GraphicsDevice tempGraphicsDevice)
        {
            SpriteGraphicsDevice = tempGraphicsDevice;
            FileName = tempPathToImg;
            SpriteTextureCreationParameters = new TextureCreationParameters(0, 0, 0, 0, SurfaceFormat.Color, TextureUsage.None, new Color(255, 163, 177), FilterOptions.None, FilterOptions.None);
            SpriteTexture = Texture2D.FromFile(SpriteGraphicsDevice, FileName, SpriteTextureCreationParameters);
            FrameCount = SpriteTexture.Width / 32;
        }

        private int _frameCount;
        private int _currentFrame = 0;
        private float _timer = 0f;
        private float _interval = 1000f / 10f;

        private Texture2D _texture2D;
        private Vector2 _spritePosition = Vector2.Zero;
        private GraphicsDevice _graphicsDevice;
        private string _fileName;
        private TextureCreationParameters _textureCreationParameters;
        private Rectangle _destRect;
        private Rectangle _sourceRect;

        public int FrameCount
        {
            get { return _frameCount; }
            set { _frameCount = value; }
        }

        public int CurrentFrame
        {
            get { return _currentFrame; }
            set { _currentFrame = value; }
        }

        public float Timer
        {
            get{return _timer;}            
            set
            {
                _timer = value;

                if (_timer > _interval)
                {
                    CurrentFrame++;
                    if (CurrentFrame > FrameCount - 1)
                    {
                        CurrentFrame = 0;
                    }
                    _timer = 0f;
                }
            }
        }

        public float Interval
        {
            get { return _interval; }
            set { _interval = value; }
        }

        public Texture2D SpriteTexture
        {
            get { return _texture2D;}
            set { _texture2D = value;}
        }

        public Vector2 SpritePosition
        {
            get { return _spritePosition;}
            set { _spritePosition = value;}
        }

        public GraphicsDevice SpriteGraphicsDevice
        {
            get { return _graphicsDevice;}
            set { _graphicsDevice = value;}
        }

        public string FileName
        {
            get { return _fileName;}
            set { _fileName = value;}
        }

        public TextureCreationParameters SpriteTextureCreationParameters
        {
            get { return _textureCreationParameters;}
            set { _textureCreationParameters = value;}
        }

        public Rectangle SpriteDestRect
        {
            get { return _destRect; }
            set { _destRect = value; }
        }

        public Rectangle SpriteSourceRect
        {
            get { return _sourceRect; }
            set { _sourceRect = value; }
        }
    }

    public class PowerSource : RobotzSprite
    {
        public PowerSource(string tempPathToImg, GraphicsDevice tempGraphicsDevice) : base(tempPathToImg, tempGraphicsDevice)
        {            
        }
    }

    public class Medic : RobotzSprite
    {
         public Medic(string tempPathToImg, GraphicsDevice tempGraphicsDevice) : base(tempPathToImg, tempGraphicsDevice)
        {            
        }
    }

    public class Engineer : RobotzSprite
    {
         public Engineer(string tempPathToImg, GraphicsDevice tempGraphicsDevice) : base(tempPathToImg, tempGraphicsDevice)
        {            
        }
    }

    public class Destroyer : RobotzSprite
    {
        public Destroyer(string tempPathToImg, GraphicsDevice tempGraphicsDevice) : base(tempPathToImg, tempGraphicsDevice)
        {            
        }
    }

    public class Wall : RobotzSprite
    {
         public Wall(string tempPathToImg, GraphicsDevice tempGraphicsDevice) : base(tempPathToImg, tempGraphicsDevice)
        {            
        }
    }

}




Advertisement
The full source code, including my childish animations, are located here:


Robotz Source Code Download on CodePlex

This topic is closed to new replies.

Advertisement