In class Player
you are wrongly creating a new Game1 theGame1 = new Game1();
instance. That means you are trying to run 2 games at once. This is not what you are trying to do. Your Game should only have 1 instance and that is in your Program.cs
file in the main method which starts running your game:
static void Main(string[] args)
{
using (Game1 game = new Game1())
{
//this is where you game begins, and the Game1() constructor
//then initialize is called, and [ Update() , Draw() ] functions keep looping
game.Run();
}
}
Remove Game1 theGame1
completely from your classes and use XNAInvaders.Game1.VariableName
instead (or you could add using XNAInvaders;
and then use Game1.VariableName
, but since you have all classes under the same namespace (XNAInvaders), you can simply use Game1.VariableName
without having to add the using
statement).
So do
void FireBullet()
{
Bullet newBullet = new Bullet(position);
newBullet.Init();
Game1.bullets.Add(newBullet);
}
So basically, every variable you have in the Game1 class can be accessed by using Game1.VariableName in any other class.
However are you sure you need a Bullet
list in your Game1
class ? You should keep your Game1.cs
as simple as possible. You could design it a bit better, using more classes(?) so that the Game1.cs
isn't that messy, it really makes a difference when your game becomes more complicated (Perhaps with using something along the lines of Level.cs
to keep the variables there)
Edit: I forgot to mention something important, you have to make your Game1 Variables static and public if you want to access them from other classes (because you will obviously have a single Game1
instance right ? So why would the variables not be static)