AerialsintheSky AerialsintheSky - 10 months ago 46
C# Question

"the name does not exist in the current context" - confused beginner

so I'm pretty new to this whole thing (as in about a week) and I'm trying to learn c# by working through the Introduction to Programming Through Game Development Using Microsoft XNA Game Studio. The section that I'm currently working on is all about adding resources to a project. I copied the instructions fairly well from what I can tell, except for changing the name of the picture they added to the name of my own picture, and currently the are 3 errors being displayed at the bottom of the screen saying various names don't exist in the current context. This book that I'm working from, which I'm basically treating as the bible, doesn't mention this sort of error happening anywhere in the section, and I'm a little cautious of reading too far ahead in case i just get more confused. So any help would be much appreciated preferably simple enough for a noob to wrap his head around.

Thanks :)

initialization bit
drawing bit

Answer Source

Both your texture and your rectangle are defined, initialized and disposed in your Init-Method. They are not accessible outside of that method. What you have to do is either pass them as method-parameters (not possible in in the Drawing-Method of XNA) or to make them "more public" and define them outside of your Init-Method:

private Texture2D starPic600Texture;
private Rectangle starPic600Rect;

protected override void LoadContent()
    spriteBatch = new SpriteBatch(GraphicsDevice);

    starPic600Rect = new Rectangle(30,20,600,600);
    starPic600Texture = this.Content.Load<Texture2D>("starPic600");

PS: Next time add your code-samples to your Question as code instead of Pictures. This would make it a Little bit easier to copy-paste and addapt your samples.