Jump to content
  • Advertisement
Sign in to follow this  

[answered] Animated Sprite not working

This topic is 2339 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Any assets will be 32 x 32
or
Sprite "sheets" will be 32 x 256 (256 wide, that is)


The animated sprite is not working properly.

What I want is to have a 32x32 block that shows the images in the following sprite sheet:

powersourceanimated1a.png


The intended result is for the power source to appear to be "glowing" -- growing brighter then getting darker then growing brighter...


I am basing all of this on a tutorial found here:

http://rbwhitaker.wi...xture-atlases-3

texture atlases?




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 XNARobotz
{
/// <summary>
/// This is the main type for your game
/// </summary>
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
AnimatedSprite animatedSprite;

private Texture2D powersource1;

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

/// <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>
protected override void LoadContent()
{
// Create a new SpriteBatch, which can be used to draw textures.
spriteBatch = new SpriteBatch(GraphicsDevice);

powersource1 = Content.Load<Texture2D>("powersource1");

Texture2D texture = Content.Load<Texture2D>("powersourceanimated1a");
animatedSprite = new AnimatedSprite(texture, 1, 8);

// TODO: use this.Content to load your game content here
}

/// <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();

// TODO: Add your update logic here

animatedSprite.Update();

base.Update(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)
{
GraphicsDevice.Clear(Color.CornflowerBlue);

// TODO: Add your drawing code here

spriteBatch.Begin();

spriteBatch.Draw(powersource1, new Rectangle(0, 0, 32, 32), Color.White);

spriteBatch.End();

animatedSprite.Draw(spriteBatch, new Vector2(100, 100));


base.Draw(gameTime);
}
}
}



AnimatedSprite class:

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

namespace XNARobotz
{
public class AnimatedSprite
{
public Texture2D Texture { get; set; }
public int Rows { get; set; }
public int Columns { get; set; }
private int currentFrame;
private int totalFrames;

public AnimatedSprite(Texture2D texture, int rows, int columns)
{
Texture = texture;
Rows = rows;
Columns = columns;
currentFrame = 0;
totalFrames = Rows * Columns;
}

public void Update()
{
currentFrame++;
if (currentFrame == totalFrames)
currentFrame = 0;
}

public void Draw(SpriteBatch spriteBatch, Vector2 location)
{
int width = Texture.Width / Rows;
int height = Texture.Height / Columns;
int row = (int)((float)currentFrame / (float)Columns);
int column = currentFrame % Columns;

Rectangle sourceRectangle = new Rectangle(width * column, height * row, width, height);
Rectangle destinationRectangle = new Rectangle((int)location.X, (int)location.Y, width, height);

spriteBatch.Begin();
spriteBatch.Draw(Texture, destinationRectangle, sourceRectangle, Color.White);
spriteBatch.End();
}
}
}

Share this post


Link to post
Share on other sites
Advertisement
How your current result looks like? Is the sprite flickering? After a quick look I would guess your Framecounter is way too fast. You go to next Spriteframe every Draw-Frame of your application. So if you have 60fps you rotate your Sprite 7,5 times. First try would be to make your currentFrame a float and add in Update just the elapsed Time. To calculate your Frame cast it to int. Then you can vary the speed of animation by dividing or multiply some value. And your animation is "time-safe" :wink:

Share this post


Link to post
Share on other sites

How your current result looks like? Is the sprite flickering? After a quick look I would guess your Framecounter is way too fast. You go to next Spriteframe every Draw-Frame of your application. So if you have 60fps you rotate your Sprite 7,5 times. First try would be to make your currentFrame a float and add in Update just the elapsed Time. To calculate your Frame cast it to int. Then you can vary the speed of animation by dividing or multiply some value. And your animation is "time-safe" :wink:


Auskennfuchs:

Thank you for your reply. Here's an update on this problem:

The two lines of code commented-out at the top were the original lines (the cause of the problem) and the next 2 lines that follow (where width and height are hard-coded) are my fix.



public void Draw(SpriteBatch spriteBatch, Vector2 location)
{
//int width = Texture.Width / Rows;
//int height = Texture.Height / Columns;

int width = 31;
int height = 31;

int row = (int)((float)currentFrame / (float)Columns);
int column = currentFrame % Columns;

Rectangle sourceRectangle = new Rectangle(width * column, height * row, width, height);
Rectangle destinationRectangle = new Rectangle((int)location.X, (int)location.Y, width, height);

spriteBatch.Begin();
spriteBatch.Draw(Texture, destinationRectangle, sourceRectangle, Color.CornflowerBlue);
spriteBatch.End();
}

Share this post


Link to post
Share on other sites
So is it working properly now?
Instead of hardcoding your height and width you can also write:

int width = Texture.Width / Rows-1;
int height = Texture.Height / Columns-1;


256/8-1 = 31
32/1-1 = 31

Share this post


Link to post
Share on other sites

So is it working properly now?
Instead of hardcoding your height and width you can also write:

int width = Texture.Width / Rows-1;
int height = Texture.Height / Columns-1;


256/8-1 = 31
32/1-1 = 31


I'll be sure to try that suggestion next chance I get (currently at work)

Tom

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!