Alright, so I'm basically trying to make a terrain, like a straight field or something. So I'll need a the width of the screen for the texture width. I tried to do it like that:
int TextureHeight = graphics.PrefferedBackBufferWidth;
Just like I did in the game1.cs class but it's not allows me to do the "graphics" Property.
I also tried to do this one:
GraphicsDevice.Viewport.TitleSafeArea.X
So I don't know what to do, maybe there's another way to draw that texture all over the screen width?
Btw what's the difference between those 2 graphics methods I mentioned?
Thanks for开发者_StackOverflow helpers.
You need a graphicsDevice, but it can be really messy to create a lot of thoses GraphicsDevices and other things. So A really simple way to pass the value of anything you want (like your screen Width) arround your game is to create a "GlobalVariable" that you can use in ANY class you want. Here is how to accomplish this easy trick.
First Create a new Class and call it GlobalClass Like this:
class GlobalClass
{
}
Now add A private variable with a public way of accessing it. Here I create two variables, one for the Screen Height and the other for the Width.
class GlobalClass
{
private static float screenWidth;
private static float screenHeight;
public static float ScreenWidth
{
get { return screenWidth; }
set { screenWidth = value; }
}
public static float ScreenHeight
{
get { return screenHeight; }
set { screenHeight = value; }
}
}
And you are done! With this class you can pass those two variables anywhere arround your game. So go back to your principal class (game1.cs) and in the update Methode Update the value of theses variables with the screen height and screen width like this:
GlobalClass.ScreenWidth = graphics.PreferredBackBufferWidth;
GlobalClass.ScreenHeight = graphics.PreferredBackBufferHeight;
now back to your custom class replace your code by this:
int TextureHeight = GlobalClass.ScreenWidth;
It's Finished, this way you can pass any value you want anywhere. Just Make sure you update the GlobalClass.ScreenWidth before you use its value.
The Game1 class contains a definition for a GraphicsDevice, as you have already used. However, your other classes that you have created do not. If you want to use the GraphicsDevice class in other classes, you will have to pass it to those classes, just like any other variable.
精彩评论