Ggcourtois

Hey,

At first I created a class for a game known as the Game of Life and I wanted to developp it on windows forms.

Then, I discovered Xna(yesterday).

So i added my existing class to a new Xna Windows game but I have some problems drawing the objects.

The code speaks for itself :

Code Snippet

Carte terrain = new Carte(5, 5);

Mouton mout = new Mouton(terrain, 0, 1);

Mouton mouti = new Mouton(terrain, 0, 4);

protected override void Draw(GameTime gameTime)

{

graphics.GraphicsDevice.Clear(Color.White);

batch.Begin(SpriteBlendMode.AlphaBlend);

foreach (Mouton m in terrain.Cells)

{

if (m != null)

{

batch.Draw(texture_mout, mout_pos, Color.White);

}

}

batch.End();

base.Draw(gameTime);

}

The error is :

Error 1 A field initializer cannot reference the nonstatic field, method, or property 'JeuDeLaLife.Game1.terrain' C:\Documents and Settings\Gabriel\My Documents\Visual Studio 2005\Projects\JeuDeLaLife\JeuDeLaLife\Game1.cs 75 34 JeuDeLaLife

Please help if you can

Thanks,

ggcourtois



Re: XNA Game Studio Express Problems implementing class in Xna Game

The ZMan

[moved to XNA forum]




Re: XNA Game Studio Express Problems implementing class in Xna Game

Bill Reiss

Move the initializers for mout and mouti into the class constructor. In this case it looks like the class is Game1, so it would look like this:

Mouton mout;

Mouton mouti;

public Game1()

{

graphics = new GraphicsDeviceManager(this);

content = new ContentManager(Services);

mout = new Mouton(terrain, 0, 1);

mouti = new Mouton(terrain, 0, 4);

}






Re: XNA Game Studio Express Problems implementing class in Xna Game

Ggcourtois

Thanks for your precious help,

I only have a problem.

Can I put this code in the initialize() function because Game1() is runned through more than once ---> creating four mouton objects instead of the two wanted Or is it not correct

ggcourtois





Re: XNA Game Studio Express Problems implementing class in Xna Game

Wojo

The problem is here:

Carte terrain = new Carte(5, 5);

Mouton mout = new Mouton(terrain, 0, 1);

You are passing terrain to an initializer but terraint is not a static variable.

If those are the two objects only in the game you can make them static like do

static Carte terrain = new Carte(5, 5);

static Mouton mout = new Mouton(terrain, 0, 1);

static Mouton mouti = new Mouton(terrain, 0, 4);