Question

I'm running into an issue where I cannot seem to get any of my textures to be recognized anywhere outside of the loadContent method.

protected override void LoadContent()
{
    spriteBatch = new SpriteBatch(GraphicsDevice);
    Texture2D tileStart = Content.Load<Texture2D>("tile_start");
    Texture2D tileCrossJunction = Content.Load<Texture2D>("tile_crossjunction");
    Texture2D tileTJunction = Content.Load<Texture2D>("tile_t-junction");
    Texture2D tileCorner = Content.Load<Texture2D>("tile_corner");
    Texture2D tileHallway = Content.Load<Texture2D>("tile_hallway");
    Texture2D tileDeadEnd = Content.Load<Texture2D>("tile_deadend");
    Texture2D sqrPlayer = Content.Load<Texture2D>("sqr_player");
    Texture2D sqrBaddieSmall = Content.Load<Texture2D>("sqr_baddie_small");
    Texture2D sqrBaddie = Content.Load<Texture2D>("sqr_baddie");
    Texture2D sqrBaddieLarge = Content.Load<Texture2D>("sqr_baddie_large");
}

No problems in this method, but when I try to reference any of these textures in my Draw method...

protected override void Draw(GameTime gameTime)
{
    GraphicsDevice.Clear(Color.DarkGray);
    base.Draw(gameTime);
    spriteBatch.Begin();
    spriteBatch.Draw(tileStart, new Vector2(0,0), Color.White);
    spriteBatch.End();
}

I get the error "tileStart does not exist in the current context."

Normally, I would say that it isn't being recognized because tileStart is a variable being declared within the LoadContent method and therefore wouldn't be usable anywhere else. The reason I'm confused is that every tutorial I've read shows this exact syntax and it seems to work fine in those cases, so clearly there's something else going on here that I don't understand.

Any help you guys can provide would be greatly appreciated, thanks.

Was it helpful?

Solution

In C#, the "scope" of variables is well defined. In your code, the textures are created within the scope of the method "LoadContent" and then are deleted once the method is done. What you need to do is place the textures at a "class" level like so:

//outside of the method, and in general, should be placed near the top of the class    
Texture2D tileStart;
Texture2D tileCrossJunction;
Texture2D tileTJunction;
Texture2D tileCorner;
Texture2D tileHallway;
Texture2D tileDeadEnd;
Texture2D sqrPlayer;
Texture2D sqrBaddieSmall;
Texture2D sqrBaddie;
Texture2D sqrBaddieLarge;

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

     //be sure to remove Texture2D from these
     //this will insure that the "class" level variables are called
     tileStart = Content.Load<Texture2D>("tile_start");
     tileCrossJunction = Content.Load<Texture2D>("tile_crossjunction");
     tileTJunction = Content.Load<Texture2D>("tile_t-junction");
     tileCorner = Content.Load<Texture2D>("tile_corner");
     tileHallway = Content.Load<Texture2D>("tile_hallway");
     tileDeadEnd = Content.Load<Texture2D>("tile_deadend");
     sqrPlayer = Content.Load<Texture2D>("sqr_player");
     sqrBaddieSmall = Content.Load<Texture2D>("sqr_baddie_small");
     sqrBaddie = Content.Load<Texture2D>("sqr_baddie");
     sqrBaddieLarge = Content.Load<Texture2D>("sqr_baddie_large");
}

Once you have done that, the "scope" of the variables will be at the class level, and you will be able to use them from other methods within the class.

In other words, there is no way to access a variable that is declared within a method from outside of that method (without passing it as a parameter to another method of course), the tutorials you are looking at might just be short hand, and expecting you to do it "properly".

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top