I am working on a Space Invaders game and I got the invader anmation set up and working. Now I only nee to create the array holding the invaders and Update/Draw it. So I got an exception on the following code:
--Game1.cs--
Variables:
botInvaders[,] InvadersArray = new botInvaders[botInvaders.BotInvaderRows, botInvaders.BotInvaderCollumns];
Update()
:
for (int y = 0; y < botInvaders.BotInvaderRows; ++y)
{
for (int x = 0; x < botInvaders.BotInvaderCollumns; ++x)
{
InvadersArray[x, y].Update(gameTime);
}
}
Draw()
:
for (int y = 0; y < botInvaders.BotInvaderRows; ++y)
{
for (int x = 0; x < botInvaders.BotInvaderCollumns; ++x)
{
InvadersArray[x, y] = new botInvaders(botInvaders.BotInvaderTex);
InvadersArray[x, y].Draw(spriteBatch);
}
}
--botInvaders.cs--
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 botInvaders
{
Texture2D Texture;
public botInvaders(Texture2D texture)
{
Texture = texture;
}
public static Texture2D BotInvaderTex;
Rectangle BotInvaderHitBox;
public static Vector2 BotInvaderPos = new Vector2(0, 28), BotInvaderOrigin;
public static int BotInvaderCurrentFrame = 1, BotInvaderFrameWidth = 52, BotInvaderFrameHeight = 88, BotInvaderRows = 10, BotInvaderCollumns = 5;
float Timer = 0f, Interval = 100f;
public void Initialize()
{
}
public void LoadContent(ContentManager Content)
{
BotInvaderTex = Content.Load<Texture2D>(".\\gameGraphics\\gameSprites\\botInvaders\\normalInvaders\\invaderShip1");
}
public void Update(GameTime gameTime)
{
BotInvaderHitBox = new Rectangle(BotInvaderCurrentFrame * BotInvaderFrameWidth, 0, BotInvaderFrameWidth, BotInvaderFrameHeight);
BotInvaderOrigin = new Vector2(BotInvaderHitBox.X / 2, BotInvaderHitBox.Y / 2);
Timer += (float)gameTime.ElapsedGameTime.Milliseconds;
if (Timer > Interval)
{
BotInvaderCurrentFrame++;
Timer = 0f;
}
if (BotInvaderCurrentFrame == 2)
{
BotInvaderCurrentFrame = 0;
}
BotInvaderHitBox = new Rectangle(BotInvaderCurrentFrame * BotInvaderFrameWidth, 0, BotInvaderFrameWidth, BotInvaderFrameHeight);
BotInvaderOrigin = new Vector2(BotInvaderHitBox.Width / 2, BotInvaderHitBox.Height / 2);
}
public void Draw(SpriteBatch spriteBatch)
{
spriteBatch.Draw(BotInvaderTex, BotInvaderPos, BotInvaderHitBox, Color.White, 0f, Vector2.Zero, 1.0f, SpriteEffects.None, 0);
}
}
}
So what could be the source of the exception?