Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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!

share|improve this question

closed as off-topic by Byte56 Feb 16 at 22:39

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions about debugging a problem in your project must present a concise selection of code and context so as to allow a reader to diagnose the issue without needing to read all of your code or to engage in extensive back-and-forth dialog. For more information, see this meta thread." – Byte56
If this question can be reworded to fit the rules in the help center, please edit the question.

1 Answer 1

up vote 0 down vote accepted

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)

share|improve this answer
    
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. –  Brein Feb 16 at 23:03
    
@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 –  Shiro Feb 16 at 23:05
    
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. –  Byte56 Feb 16 at 23:54
    
@Byte56 oh ok, I guess answering off topic questions encourages people to ask them, I will know from now on –  Shiro Feb 17 at 0:00

Not the answer you're looking for? Browse other questions tagged or ask your own question.