0

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.

2 Answers 2

1

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?

Sign up to request clarification or add additional context in comments.

3 Comments

Hey, making the texture2d variable and its property static did work, thanks alot! Concerning the Platformer03 class: I find the class, but its not static. Therefore I would want to find the instance of the class, called "game". The program.cs' Main method reads as follows: using (Platformer03 game = new Platformer03()) { game.Run(); } Now according to MSDN, using "Defines a scope, outside of which an object or objects will be disposed." Is that why I can't "see" the "game" instance of Platformer03 in other classes? (Namespace is the same everywhere) If so, can I change the using?
The class itself shouldn't be static, only the variable. You can then access the static variable without knowing the instance of the class (since it's static, meaning it's shared between all instances of the class). You can only "see" an instance of an object if you pass it (in this case, your game variable) around.
Until now I only used static variables in static classes, thanks for widening my horizon (if thats a phrase at all)!! :D Everything's fine now, thanks again!
1

I'm not sure I understand your question, but if in your Platformer03 constructor you are already loading the wanted texture, you can just instantiate a new Platformer03 and get it. Like this:

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

or if in your Plataformer03 you will use only one value in Texture for all diferents objects of Platformer03, you can have a static member inside a non static class, like this:

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

Sorry about my English.

2 Comments

Yes, the first one I thought of, too, but since "platformer03" is the game, basically, I didn't want a second one (for now), I imagine it would not be a clean solution to create a whole new game instance to get the texture 'out'. Your second suggestion is the one I picked now, as far as I understood, it's what Willem Duncan suggested, right? One last question, totally unrelated to my main question: You use "this.texture", and while I understand why you use it, is it necessary? Concerning your English: I'm 'foreigner', too, so no worries! As long as we all understand each other all is good.
we normally use this to represent current object of a class (it's more to keep the code clear and nice, and for don't have problems with 2 variables with same name because you can create a Texture to the class and another to the method, in this case to call the Texture of the class you will need the this)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.