XNA c# Sprite remain after mouseclick - c#

I have a question.
Is there a way to make sprite remain after mouseclick on it without making a List of sprites? I mean, my code generates sprite randomly at the screen and if I hit red sprite it dissappear. How to make red sprites stay on the screen after click?
Here is my code:
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 Ghost
{
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
Texture2D ghostPic;
Texture2D background;
Random rand;
int ghostColour;
Rectangle ghostLocation;
float timeRemaining;
float showTime;
int score;
MouseState mouseState, mouseStatePrevious;
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}
protected override void Initialize()
{
graphics.PreferredBackBufferHeight = 550;
graphics.PreferredBackBufferWidth = 750;
this.IsMouseVisible = true;
this.Window.Title = "Ghost Hunt | Score: 0";
this.Window.AllowUserResizing = true;
showTime = 0.5f;
rand = new Random();
base.Initialize();
}
protected override void LoadContent()
{
// Create a new SpriteBatch, which can be used to draw textures.
spriteBatch = new SpriteBatch(GraphicsDevice);
ghostPic = Content.Load<Texture2D>("GhostPic");
background = Content.Load<Texture2D>("Background");
// TODO: use this.Content to load your game content here
}
protected override void UnloadContent()
{
// TODO: Unload any non ContentManager content here
}
protected override void Update(GameTime gameTime)
{
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();
if (Keyboard.GetState(PlayerIndex.One).IsKeyDown(Keys.Escape))
this.Exit();
mouseState = Mouse.GetState();
if (mouseState.LeftButton == ButtonState.Pressed && mouseStatePrevious.LeftButton == ButtonState.Released && ghostLocation.Contains(mouseState.X, mouseState.Y))
{
if (ghostColour == 0) score++;
if (ghostColour == 1)
{
score--;
}
this.Window.Title = "Ghost Hunt | Score: " + score.ToString();
timeRemaining = 0.0f;
}
else
{
timeRemaining = MathHelper.Max(0, timeRemaining - (float)gameTime.ElapsedGameTime.TotalSeconds);
}
mouseStatePrevious = mouseState;
if (timeRemaining == 0.0f)
{
ghostColour = rand.Next(2);
ghostLocation = new Rectangle(rand.Next(GraphicsDevice.Viewport.Bounds.Width - ghostPic.Width), rand.Next(GraphicsDevice.Viewport.Bounds.Height - ghostPic.Height), ghostPic.Width / 2, ghostPic.Height / 2);
timeRemaining = showTime;
}
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
spriteBatch.Begin();
spriteBatch.Draw(background, new Rectangle(0,0,GraphicsDevice.Viewport.Bounds.Width,GraphicsDevice.Viewport.Bounds.Height), Color.White);
switch (ghostColour)
{
case 0:
spriteBatch.Draw(ghostPic, ghostLocation, Color.White * 0.7f);
break;
case 1:
spriteBatch.Draw(ghostPic, ghostLocation, new Color(255,0,0,50));
break;
}
spriteBatch.End();
base.Draw(gameTime);
}
}
}

I suppose you could make an array like Sprite[] instead of a List but the redraw will still clear your screen and redraw it entirely, clearing any non-stored drawables.
What could be fun is making a LinkedList instead where you keep the last 10 sprites saved, throw away the first when 10 is reached. This would allow you to draw a trail of sprites that follows the position of your current sprite drawing.

Related

C# bouncing cubes game, remove cubes from 100px of the center

I am making a small "game" in Monogame with C# of 2000 blocks that bounce around the screen. I need to make the cubes disappear if they are within a circle 100px of the center of the screen. The 2 classes i have are (Game1.cs) which is the game itself, and then (Block.cs) which is the code for the blocks bouncing around the screen, here is the code for both:
Game1.cs:
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using System;
using System.Collections.Generic;
namespace Inlämning2
{
public class Game1 : Game
{
private GraphicsDeviceManager _graphics;
private SpriteBatch _spriteBatch;
Texture2D pixelTexture;
List<Block> blocks = new List<Block>();
public Game1()
{
_graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
IsMouseVisible = true;
}
protected override void Initialize()
{
Random rnd = new Random();
// TODO: Add your initialization logic here
for (int i = 0; i < 1000; i++)
{
var block = new Block();
block.X = rnd.Next(0, 600);
block.Y = rnd.Next(0, 400);
block.Color = new Color(rnd.Next(256), rnd.Next(256), rnd.Next(256));
blocks.Add(block);
}
base.Initialize();
}
protected override void LoadContent()
{
// TODO: use this.Content to load your game content here
_spriteBatch = new SpriteBatch(GraphicsDevice);
pixelTexture = Content.Load<Texture2D>("pixel");
}
protected override void Update(GameTime gameTime)
{
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
Exit();
foreach (Block block in blocks)
{
block.Update();
}
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
_spriteBatch.Begin();
foreach (Block block in blocks)
{
block.Draw(_spriteBatch, pixelTexture);
}
_spriteBatch.End();
base.Draw(gameTime);
}
}
}
---------------------------------------------Block.cs:--------------------------------------------
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
namespace Inlämning2
{
class Block
{
public int X { get; set; } = 100;
public int Y { get; set; } = 100;
public Color Color { get; set; } = Color.Red;
public int speed1 = 1;
public int speed2 = -1;
public void Draw(SpriteBatch spriteBatch, Texture2D texture)
{
spriteBatch.Draw(texture, new Rectangle(X, Y, 30, 30), Color);
}
public void Update()
{
if (X< 0) speed2 += 1;
if (X > 770) speed2 += -1;
if (Y > 450) speed1 += -1;
if (Y< 0) speed1 += 1;
X += speed2;
Y += speed1;
if (speed1 > 1) speed1 = 1;
if (speed2 > 1) speed2 = 1;
}
}
}
Calculate the center of the screen as a Vector2 called Center.
//pseudo C# code:
if (Center - X,Y).LengthSquared() < 100 * 100) //fix this line
... then remove block Blocks.Remove(this), not possible during an enumeration, find another way.
If the question is referring to Manhattan Distance the formula: if((Math.Abs(Center.X -X) + Math.Abs(Center.Y -Y))< 100)
would apply.

Making a Texture2D move in the direction of a mouse click with XNA?

This is the code I have so far. I'm trying to get the bullet to move in the direction of where the mouse is clicked. I have a bullet Rectangle, and a Texture2D, and the same goes for the cannon from where the bullet is fired.
namespace Targeted
{
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
int screenWidth;
int screenHeight;
int speedX;
int speedY;
MouseState oldMouse;
Texture2D cannon;
Rectangle cannonRect;
Texture2D bullet;
Rectangle bulletRect;
KeyboardState kb;
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}
protected override void Initialize()
{
// TODO: Add your initialization logic here
screenWidth = graphics.GraphicsDevice.Viewport.Width;
screenHeight = graphics.GraphicsDevice.Viewport.Height;
oldMouse = Mouse.GetState();
speedX = 0;
speedY = 0;
cannonRect = new Rectangle((screenWidth / 2) - 100, (screenHeight / 2) - 100, 100, 100);
bulletRect = new Rectangle(cannonRect.X, cannonRect.Y, 10, 10);
this.IsMouseVisible = true;
base.Initialize();
}
protected override void LoadContent()
{
// Create a new SpriteBatch, which can be used to draw textures.
spriteBatch = new SpriteBatch(GraphicsDevice);
cannon = Content.Load<Texture2D>("Smoothed Octagon");
bullet = Content.Load<Texture2D>("White Square");
// TODO: use this.Content to load your game content here
}
protected override void UnloadContent()
{
// TODO: Unload any non ContentManager content here
}
protected override void Update(GameTime gameTime)
{
bulletRect.X += speedX;
bulletRect.Y += speedY;
this.IsMouseVisible = true;
// Allows the game to exit
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || kb.IsKeyDown(Keys.Escape))
this.Exit();
// TODO: Add your update logic here
MouseState mouse = Mouse.GetState();
kb = Keyboard.GetState();
if (mouse.LeftButton == ButtonState.Pressed && oldMouse.LeftButton == ButtonState.Released)
{
bulletRect.X += speedX;
bulletRect.Y += speedY;
}
oldMouse = mouse;
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.Black);
// TODO: Add your drawing code here
spriteBatch.Begin();
spriteBatch.Draw(bullet, bulletRect, Color.White);
spriteBatch.Draw(cannon, cannonRect, Color.White);
spriteBatch.End();
base.Draw(gameTime);
}
}
}
Here's some pseudocode of how to handle bullet velocity and orientation relative to a point.
// On MouseClick
float angle = Math.Atan2(mouseClick.X - player.X, mouseClick.Y - player.Y);
bulletVelocity.X = (Math.Cos(angle) + Math.Sin(angle)) * bulletSpeed;
bulletVelocity.Y = (-Math.Sin(angle) + Math.Cos(angle)) * bulletSpeed;
// On Update Positions
bullet.X += bulletVelocity.X;
bullet.Y += bulletVelocity.Y;
For static entities like cannons, replace "player" with "cannon" and "mouseClick" with "player".
(I'm recalling the positions of Sin and Cos from memory, so hopefully someone can correct me if that's the wrong setup.)

How to generate infinitly enemies in XNA? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I was developing a game using XNA frame work and here is my code
I just want to generate infinite number of enemies
here is my code please help
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 fight_in_the_sky
{
/// <summary>
/// This is the main type for your game
/// </summary>
///
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spritebatch;
Texture2D anim, background1, background2, enemy;
Rectangle rect0, rect2, rectdst, srcrect, rect3, rect4, srcrect2, enemy_rect;
float elapsed;
float delay = 40f;
int frames = 0;
Random random = new Random();
public Vector2 velocity;
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
graphics.PreferredBackBufferWidth = 800;
graphics.PreferredBackBufferHeight = 600;
IsMouseVisible = true;
}
protected override void Initialize()
{
// TODO: Add your initialization logic here
rectdst = new Rectangle(0, 250, 115, 69);
rect3 = new Rectangle(0,0,1280,720);
rect4 = new Rectangle(1280, 0, 1280, 720);
enemy_rect = new Rectangle(random.Next(800-94,800-47),random.Next(600-122,600-61),376/8,61);
velocity.X = 3f;
velocity.Y = 3f;
base.Initialize();
}
protected override void LoadContent()
{
// Create a new SpriteBatch, which can be used to draw textures.
spritebatch = new SpriteBatch(GraphicsDevice);
background1= Content.Load<Texture2D>("starfield006");
background2= Content.Load<Texture2D>("starfield005");
anim=Content.Load<Texture2D>("shipAnimation");
enemy =Content.Load<Texture2D>("mineAnimation");
rect0 = new Rectangle(0, 0, 800, 600);
rect2 = new Rectangle(0, 250,200, 100);
// TODO: use this.Content to load your game content here
}
protected override void UnloadContent()
{
// 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();
// TODO: Add your update logic here
elapsed += (float)gameTime.ElapsedGameTime.Milliseconds;
if (Keyboard.GetState().IsKeyDown(Keys.Left))
{
rectdst.X -= 12;
if (rectdst.X < 0) { rectdst.X = 0; }
}
if (Keyboard.GetState().IsKeyDown(Keys.Right))
{
rectdst.X += 12;
if (rectdst.X > 700) { rectdst.X = 700; }
}
if (Keyboard.GetState().IsKeyDown(Keys.Up))
{
rectdst.Y -= 12;
if (rectdst.Y < 0) { rectdst.Y = 0; }
}
if (Keyboard.GetState().IsKeyDown(Keys.Down))
{ rectdst.Y += 10; if (rectdst.Y > 550) { rectdst.Y = 550; } }
base.Update(gameTime);
if (elapsed >= delay)
{
if (frames >= 7)
{ frames = 0; }
else { frames++; }
elapsed = 0;
}
srcrect = new Rectangle(115 * frames, 0, 115, 69);
srcrect2 = new Rectangle(47*frames,0,47,61);
if (rect3.X + background1.Width <= 0)
rect3.X = rect4.X + background2.Width;
if (rect4.X + background2.Width <= 0)
rect4.X = rect3.X + background1.Width;
rect3.X -= 5;
rect4.X -= 5;
enemy_rect.X +=(int) velocity.X;
enemy_rect.Y += (int)velocity.Y;
if (enemy_rect.Y >= 600 - 61 || enemy_rect.Y <= 0) velocity.Y = -velocity.Y;
if (enemy_rect.X >= 800 - 47) velocity.X = -velocity.X;
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
// TODO: Add your drawing code here
spritebatch.Begin();
spritebatch.Draw(background1,rect3, Color.Azure);
spritebatch.Draw(background2,rect4, Color.Azure);
spritebatch.Draw(anim, rectdst, srcrect, Color.White);
spritebatch.Draw(enemy, enemy_rect, srcrect2, Color.White);
spritebatch.End();
base.Draw(gameTime);
}
}
}
and here is a shot of my current output
http://imgur.com/7y43S7O
Infinite is unpractical to build into a game, because sooner or later you would either run out of RAM or your screen would be too full to call it a game. Anyway, I can give you a basic idea to work with.
If you want your enemy count to keep increasing you can use a list and add an enemy every time you want one to appear
List<Enemy> enemies = new List<Enemy>;
In Update:
if(spawnTimer > spawnInterval && enemies.Count()<enemyLimit){
enemies.add(new Enemy([rectangle of spawn location and image size]);
spawnTimer = 0;
}
for(int i=0; i<enemies.Count();i++){
if(enemies[i].defeated){
enemies.Remove(enemies[i]);
}
spawnTimer+=gameTime.ElapsedGameTime.TotalSeconds;
in Draw:
for(int i=0; i<enemies.Count();i++){
spritebatch.Draw(enemy, enemies[i].Rect, srcrect2, Color.White);
}
class Enemy{
public Rectangle Rect{get;set;}
public bool defeated{get;set;}
public int Health{get;set;}
public Enemy(Rectangle rect){
Rect = rect;
Health = 100;
defeated = false;
}
}

Attempting to get asteroids to move down screen themselves

I'm attempting to create an asteroids game and what I am trying to do is to get the asteroids to move down the screen themselves. The code I have loads the asteroids onto the screen however the asteroids do not move.
The code I have for the movement of the asteroids is listed below.
public class Asteroids
{
public int speed;
public Asteroids(Texture2D newTexture, Vector2 newPosition)
{
speed = 2;
}
public LoadContent()
{
while (asteroidsList.Count() < 5)
{
randX = random.Next(0, 1000) + speed;
randY = random.Next(-200, 984) + speed;
asteroidsList.Add(new Asteroids(Content.Load<Texture2D>("asteroid big"), new Vector2(randX, randY)));
}
}
public void Update (GameTime gameTime)
{
// Update Origin
if (texture != null)
{
Asteroidorigin.X = texture.Width / 2;
Asteroidorigin.Y = texture.Height / 2;
}
foreach (Asteroids a in asteroidsList)
{
position.Y = position.Y + speed;
position.X = position.X + speed;
}
if (position.X >= 1280)
{
position.X = -105;
}
if (position.Y >= 1024)
{
position.Y = -105;
}
}
}
Game1.cs
namespace AsteroidsGame
{
// Main
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
Random random = new Random();
// List Of Asteroids
// Making New Objects Of These Classes
Player p = new Player();
Background bg = new Background();
Asteroids a = new Asteroids();
EnemySpaceship es = new EnemySpaceship();
// Constructor
public Game1()
{
graphics = new GraphicsDeviceManager(this);
graphics.IsFullScreen = false; // Fullscreen mode
graphics.PreferredBackBufferWidth = 1280; // Screen Width
graphics.PreferredBackBufferHeight = 1024; // Screen Height
this.Window.Title = "12013951 Asteroids Game";
Content.RootDirectory = "Content";
}
// Init
protected override void Initialize()
{
base.Initialize();
}
// Load Content
protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
bg.LoadContent(Content);
p.LoadContent(Content);
a.LoadContent(Content);
es.LoadContent(Content);
}
// Unload Content
protected override void UnloadContent()
{
}
// Update
protected override void Update(GameTime gameTime)
{
// Allows the game to exit
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();
bg.Update(gameTime);
p.Update(gameTime);
a.Update(gameTime);
es.Update(gameTime);
a.CheckCollisionAsteroid();
base.Update(gameTime);
}
// Draw
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
spriteBatch.Begin();
bg.Draw(spriteBatch); //Draws The Background
p.Draw(spriteBatch); // Draws The Player
a.Draw(spriteBatch); // Draws asteroids
es.Draw(spriteBatch); // Draws enemy spaceships
spriteBatch.End();
base.Draw(gameTime);
}
// Load Asteroids
}
}
Any help with this issue would be greatly appreciated.
You are modifying the position of something, but I don't see how modifying that position property is modifying the position of each asteroid. You need each asteroid to have its own position property. You could try something like this (assuming position is a Vector2 property of the asteroid class):
public void Update (GameTime gameTime)
{
foreach (Asteroids a in asteroidsList)
{
a.position = new Vector2(
MathHelper.Clamp(a.position.X + speed, -105, 1280),
MathHelper.Clamp(a.position.Y + speed, -200, 1024));
}
}
The MathHelper.Clamp() also removes the need for the if statements you have in the update method.

Slow down texture changes in an array when dealing with a sprite.

I am making a little game in c# using XNA 4.0. I have successfully made my sprite move in all directions and make it look like it is actually walking using an array. So far I have only tested this with the "Up" key. The problem is that when I push the up key, the sprite moves, but it goes through all the elements in the array so fast that it looks like he is running way to fast for the amount of space he is going. Is there any way to slow down the speed at which the textures change between each other, such as a pause method or something. Any bit of help is appreciated, Thanks.
namespace RandomGame
{
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
Color backColor = Color.FromNonPremultiplied(190, 230, 248, 250);
int i = 0;
public Game1()
{
graphics = new GraphicsDeviceManager(this);
graphics.IsFullScreen = false;
graphics.PreferredBackBufferHeight = 500;
graphics.PreferredBackBufferWidth = 800;
Content.RootDirectory = "Content";
}
protected override void Initialize()
{
base.Initialize();
}
Texture2D[] UpTextures = new Texture2D[6];
Texture2D startTexture;
Texture2D leftTexture;
Texture2D rightTexture;
Vector2 position = new Vector2(380, 230);
protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
startTexture = Content.Load<Texture2D>("BlueLinkStart");
leftTexture = Content.Load<Texture2D>("BlueLinkLeft");
rightTexture = Content.Load<Texture2D>("BlueLinkRight");
UpTextures[0] = Content.Load<Texture2D>("BlueLinkUp");
UpTextures[1] = Content.Load<Texture2D>("BlueLinkUp2");
UpTextures[2] = Content.Load<Texture2D>("BlueLinkUp3");
UpTextures[3] = Content.Load<Texture2D>("BlueLinkUp4");
UpTextures[4] = Content.Load<Texture2D>("BlueLinkUp5");
UpTextures[5] = Content.Load<Texture2D>("BlueLinkUp6");
}
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();
if (Keyboard.GetState(PlayerIndex.One).IsKeyDown(Keys.Escape))
{
this.Exit();
}
if (Keyboard.GetState(PlayerIndex.One).IsKeyDown(Keys.Left) && position.X > -3)
{
position.X -= 2;
}
if (Keyboard.GetState(PlayerIndex.One).IsKeyDown(Keys.Right) && position.X < 772)
{
position.X += 2;
}
if (Keyboard.GetState(PlayerIndex.One).IsKeyDown(Keys.Up) && position.Y > -3)
{
position.Y -= 2;
}
if (Keyboard.GetState(PlayerIndex.One).IsKeyDown(Keys.Down) && position.Y < 472)
{
position.Y += 2;
}
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.FromNonPremultiplied(188, 231, 241, 255));
if (Keyboard.GetState(PlayerIndex.One).IsKeyDown(Keys.Left))
{
spriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend);
spriteBatch.Draw(leftTexture, position, Color.White);
spriteBatch.End();
base.Draw(gameTime);
}
else if (Keyboard.GetState(PlayerIndex.One).IsKeyDown(Keys.Right))
{
spriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend);
spriteBatch.Draw(rightTexture, position, Color.White);
spriteBatch.End();
base.Draw(gameTime);
}
else if (Keyboard.GetState(PlayerIndex.One).IsKeyDown(Keys.Up))
{
spriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend);
spriteBatch.Draw(UpTextures[i], position, Color.White);
spriteBatch.End();
i++;
if (i == 6) { i = 0; }
base.Draw(gameTime);
}
else
{
spriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend);
spriteBatch.Draw(startTexture, position, Color.White);
spriteBatch.End();
base.Draw(gameTime);
}
}
}
}
Yes, there is.
First of all, you need a variable that holds the animation state. Your i is probably supposed to be that. However, you should rename it to e.g. animationState to reflect its purpose. Furthermore, it is easier to make it a float or double variable.
Then updating the animation is a task for the Update() method. You obviously rely on the 60 Hz update frequency. This is ok for a small game, but you should take possible slow downs into consideration for bigger ones. If you have n sprites, and you want to change the sprite every m ms, then you update the animationState as follows:
animationState += updateDuration * m;
if(animationState >= n) animationState -= n;
updateDuration is the time since the last update. So for 60 Hz this is 1000.0 / 60.
And then you need to draw the correct sprite in the Draw() method:
spriteBatch.Draw(UpTextures[(int)animationState], position, Color.White);

Categories

Resources