0
\$\begingroup\$

Hi all i started creating this XNA/C# version of Space invaders to get better at xna since i've only been using it for about 4 days.

I've managed fine till now..

So my problem is adding my Bullets to a List in another class. So i've tried working out the solution for about 3 hours now and i hope you guys can help me.

In the Player.cs class i've got a method FireBullet() wich should create a new Bullet.cs class and add it to the list in Game1.cs (it does not do this when you debug) in theory this should work(i think).

Linking all the corresponding classes will take too much room so i've uploaded it to github 

https://github.com/Breinss/Space-Invaders-XNA-C-

says C in title ran out of character ment to be C# ^^

Thanks in advanced!

\$\endgroup\$

1 Answer 1

0
\$\begingroup\$

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)

\$\endgroup\$
4
  • \$\begingroup\$ You're the champ i litteraly looked at the code for 2-3 hours and you fix it in a few minutes. And yes i am going to refactor the Game1 class once i'm done adding the basic functionality. \$\endgroup\$
    – Brein
    Feb 16, 2015 at 23:03
  • \$\begingroup\$ @Brein i downloaded your project and run it and i just want to add that you need previousKeyboardState and currectKeyboardState for the SpaceBar to work as you expect it to \$\endgroup\$
    – dimitris93
    Feb 16, 2015 at 23:05
  • \$\begingroup\$ Please don't answer off topic questions like this. If you want to help, ask OP to take the question to chat or a discussion oriented forum where one-on-one help like this can take place. \$\endgroup\$
    – House
    Feb 16, 2015 at 23:54
  • \$\begingroup\$ @Byte56 oh ok, I guess answering off topic questions encourages people to ask them, I will know from now on \$\endgroup\$
    – dimitris93
    Feb 17, 2015 at 0:00

Not the answer you're looking for? Browse other questions tagged .