I have a problem which I need to resolve. I am making a Space Invaders game in C# XNA. I've been following this tutorial for the Invaders: https://www.youtube.com/watch?list=LLhsF03QfDMDhbw02_8C6Smg&v=wIW4S75iZBI&feature=player_detailpage#t=397 But I also have my own code for animating the Invader spritesheet. And eventually here's what I'm drawing:
void Draw(SpriteBatch spriteBatch){
for (int r = 0; r < m_InvaderRows; r++)
{
for (int c = 0; c < m_InvaderCollumns; c++)
{
spriteBatch.Draw(m_BotInvaderTex, m_BotInvaderPos, m_BotInvaderHitBox, Color.White, 0f, Vector2.Zero, 1.0f, SpriteEffects.None, 0);
// ???
spriteBatch.Draw(m_BotInvaderTex, m_BotInvadersRect[r, c], Color.White);
}
}
}
So the first Draw()
call draws only 1 animated sprite. The second draws 50 non-animated sprites. m_BotInvadersRect[r, c]
is a rectangle I used as a multi-dimensional array. So I'm looking for a way to combine both Draw
calls and have 50 animated sprites drawn on my screen.
How can I animate the Invaders using a the code bellow?
My code:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Storage;
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;
namespace SpaceInvaders
{
class botInvader
{
public botInvader()
{
}
public static Texture2D g_BotInvaderTex;
Rectangle m_BotInvaderHitBox;
public static Vector2 g_BotInvaderPos = new Vector2(0, 24);
Vector2 m_BotInvaderOrigin;
int m_BotInvaderCurrentFrame = 1;
int m_BotInvaderFrameWidth = 52;
int m_BotInvaderFrameHeight = 88;
float m_Timer = 0f;
float m_Interval = 100;
Rectangle[,] m_BotInvadersRect;
int m_InvaderRows = 5;
int m_InvaderCollumns = 10;
String m_BotInvadersDirection = "RIGHT";
public void Initialize()
{
}
public void LoadContent(ContentManager Content)
{
g_BotInvaderTex = Content.Load<Texture2D>(".\\gameGraphics\\gameSprites\\botInvaders\\normalInvaders\\spritesheet"); // invaderShip1
m_BotInvadersRect = new Rectangle[m_InvaderRows, m_InvaderCollumns];
for (int r = 0; r < m_InvaderRows; r++)
{
for (int c = 0; c < m_InvaderCollumns; c++)
{
m_BotInvadersRect[r, c].Width = g_BotInvaderTex.Width;
m_BotInvadersRect[r, c].Height = g_BotInvaderTex.Height;
m_BotInvadersRect[r, c].X = 70 * c;
m_BotInvadersRect[r, c].Y = 70 * r;
}
}
}
public void Update(GameTime gameTime)
{
m_BotInvaderHitBox = new Rectangle(m_BotInvaderCurrentFrame * m_BotInvaderFrameWidth, 0, m_BotInvaderFrameWidth, m_BotInvaderFrameHeight);
m_BotInvaderOrigin = new Vector2(m_BotInvaderHitBox.X / 2, m_BotInvaderHitBox.Y / 2);
m_Timer += (float)gameTime.ElapsedGameTime.Milliseconds;
if (m_Timer > m_Interval)
{
m_BotInvaderCurrentFrame++;
m_Timer = 0f;
}
if (m_BotInvaderCurrentFrame == 2)
{
m_BotInvaderCurrentFrame = 0;
}
m_BotInvaderHitBox = new Rectangle(m_BotInvaderCurrentFrame * m_BotInvaderFrameWidth, 0, m_BotInvaderFrameWidth, m_BotInvaderFrameHeight);
m_BotInvaderOrigin = new Vector2(m_BotInvaderHitBox.Width / 2, m_BotInvaderHitBox.Height / 2);
int m_RightSide = 800;
int m_LeftSide = 0;
for (int r = 0; r < m_InvaderRows; r++)
{
for (int c = 0; c < m_InvaderCollumns; c++)
{
if (m_BotInvadersDirection.Equals ("RIGHT"))
{
m_BotInvadersRect[r, c].X = m_BotInvadersRect[r, c].X + 1;
}
if (m_BotInvadersDirection.Equals("LEFT"))
{
m_BotInvadersRect[r, c].X = m_BotInvadersRect[r, c].X - 1;
}
}
}
String m_BotInvadersChangeDirection = "N";
for (int r = 0; r < m_InvaderRows; r++)
{
for (int c = 0; c < m_InvaderCollumns; c++)
{
if (m_BotInvadersRect[r, c].X + m_BotInvadersRect[r, c].Width > m_RightSide)
{
m_BotInvadersDirection = "LEFT";
m_BotInvadersChangeDirection = "Y";
}
if (m_BotInvadersRect[r, c].X < m_LeftSide)
{
m_BotInvadersDirection = "RIGHT";
m_BotInvadersChangeDirection = "Y";
}
}
if (m_BotInvadersChangeDirection.Equals("Y"))
{
for (int c = 0; c < m_InvaderCollumns; c++)
{
m_BotInvadersRect[r, c].Y = m_BotInvadersRect[r, c].Y + 3;
}
}
}
}
public void Draw(SpriteBatch spriteBatch)
{
for (int r = 0; r < m_InvaderRows; r++)
{
for (int c = 0; c < m_InvaderCollumns; c++)
{
spriteBatch.Draw(g_BotInvaderTex, g_BotInvaderPos, m_BotInvaderHitBox, Color.White, 0f, Vector2.Zero, 1.0f, SpriteEffects.None, 0);
spriteBatch.Draw(g_BotInvaderTex, m_BotInvadersRect[r, c], Color.White);
}
}
}
}
}