views:

140

answers:

4

I am making a game using C# with the XNA framework. The player is a 2D soldier on screen and the user is able to fire bullets. The bullets are stored in an array. I have looked into using Lists and arrays for this and I came to the conclusion that an array is a lot better for me, as there will be a lot of bullets firing and being destroyed at once, something that I read Lists don't handle so well.

After reading through some posts on the XNA forums, this came to my attention: http://forums.xna.com/forums/p/16037/84353.aspx

I have created a struct like so:

// Bullets
struct Bullet
{
    Vector2 Position;
    Vector2 Velocity;
    float Rotation;
    Rectangle BoundingRect;
    bool Active;
}

And I made the array like this:

Bullet[] bulletCollection = new Bullet[100];

But when I try to do some code like this:

    // Fire bullet
    if (mouseState.LeftButton == ButtonState.Pressed)
    {
        for (int i = 0; i < bulletCollection.Length; i++)
        {
            if (!bulletCollection[i].Active)
            {
                // something
            }
        }

I get the following error:

'Zombie_Apocalypse.Game1.Bullet.Active' is inaccessible due to its protection level

Can anyone lend a hand? I have no idea why this error is popping up, or even if I'm declaring the array properly or anything... as the post on the XNA forums doesn't go into detail about that.

Thank you for any help you can provide. :)

+7  A: 

I believe C# makes members private by default, so you'd simply need to make Active a public field:

public bool Active;

More advisable would be to make it a property with a public get accessor, keeping the set private:

public bool Active { get; private set; }
Dan Tao
Hah, that should have been obvious xD Thanks. Am I declaring my array correctly though? C# doesn't through any errors when compiling but I'm still not sure.
Joshua
Also I have to wait 9 minutes before I accept your answer, but I will do when I can. :)
Joshua
@Whitey: You are declaring your array correctly. Since `Bullet` is a struct, the array is filled with default values (just as an `int[]` would be filled with zeros); if you had made it a class, the array would be full of nulls and you would need to instantiate each bullet.
Dan Tao
@Dan Tao, using properties on stucts is not a rule to be followed in XNA (even the class library doesn't use it) because of performance implications. Actually the rule is not to use properties unless encapsulation *is* needed, so you can eliminate redundant copying of data.
Pop Catalin
+4  A: 

Try adding public

public struct Bullet
{
    public Vector2 Position;
    public Vector2 Velocity;
    public float Rotation;
    public Rectangle BoundingRect;
    pubilc bool Active;
}
brickner
+2  A: 

C# makes all class member private by default - you need to specify the access modifiers:

public struct Bullet
{
  public Vector2 Position;
  public Vector2 Velocity;
  public float Rotation;
  public Rectangle BoundingRect;
  public bool Active;
}
Oded
A: 

I believe making the struct itself public is all that's necessary, since struct fields default to public while class fields default to private. However, as others suggest, explicitly making your fields public is good practice: write what you mean, instead of letting the language assume for you! :)

Brian S