Search code examples
c#constructorinitializationxna-4.0texture2d

Initializing a non-static class with a texture2d variable


I'm learning XNA (and C# in general), and while trying to write my own little sidescroller for learning purposes, I stumbled on the following problem.

The level is build from tiles, and since I don't want to pass the texture to the tile on every instantiation (like return new Tile(positionVector2, TEXTURE)) the class should be constructed with this texture by default.

Now I just can't get it to work, I tried to use the constructor like this:

public Tile(Vector2 position)
{
   this.texture = Platformer03.Texture;  
}

where Platformer03 is the game class and Texture the property for the loaded tile-texture. Now since Platformer03 is the class and not the instance (its not static), this obviously won't work, but I can't even find the instance of Platformer03 (its called game1, but is out of scope).

So I'm sure I'm doing this all wrong, but how can I get the Tile class to use a certain texture2d on each instantiation?

Please be gentle, even though you probably can't imagine a more idiotic question, I can assure you that I've googled and read like a madman for the last few hours to figure this out.


Solution

  • If you make the Texture variable under Platformer03 into a static variable, then it will be accessible without having an instance of the Platformer03 class itself (provided it's not marked private or protected).

    If you cannot find the Platformer03 class, maybe you're not looking in the correct namespace?