问题
I make a class Resize.cs
and that takes an image from the Images.cs
class and finds the width and height of the image by creating a new integer for each the width and height. I also make a resize to change the size of the image to fit the screen.
public static int resize = 1;
public static int backgroundWidth = resize * Images.Background.Width;
public static int backgroundHeight = resize * Images.Background.Height;
Then I take the width and height and move it over to the Game1.cs
(The main class) and in the public Game1()
I change the width and height of the screen, that works:
public static int 768;
public static int screenWidth = 1024;
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
graphics.PreferredBackBufferHeight = screenHeight;
graphics.PreferredBackBufferWidth = screenWidth;
graphics.IsFullScreen = false;
graphics.ApplyChanges();
StartUp.Images.Content = Content;
}
To:
public static int screenHeight = StartUp.Resize.BackgroundHeight;
public static int screenWidth = StartUp.Resize.BackgroundWidth;
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
graphics.PreferredBackBufferHeight = screenHeight;
graphics.PreferredBackBufferWidth = screenWidth;
graphics.IsFullScreen = false;
graphics.ApplyChanges();
StartUp.Images.Content = Content;
}
But no matter what I do, the Program.cs (That I never touch) comes up with an error: (This only happens when I start up the program it will say it is fine befor and after the program is started and ended)
TypeInitializationException was unhandled The type initializer for 'Love__Regret.Game1' threw an exception.
using System;
namespace **
{
#if WINDOWS || XBOX
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
static void Main(string[] args)
{
using (Game1 game = new Game1())
{
game.Run();
}
}
}
#endif
}
In the end my goal is to re size the screen at will inside the program. (because if 2 computers with different sizes of screen use the program it should fit correctly in both)
I inserted the regular numbers height = 100 and width = 157, they worked fine and resized the screen just as it should.
Images.cs
:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
namespace **.StartUp
{
class Images
{
#region Define
public static ContentManager Content;
//TitleScreen
public static Texture2D Background;
public static Texture2D Continue;
public static Texture2D Credits;
public static Texture2D Exit;
public static Texture2D Logo;
public static Texture2D Options;
public static Texture2D Play;
public static Texture2D Version;
//End
//Options
public static Texture2D OptionsLogo;
public static Texture2D OffNotSelected;
public static Texture2D OffSelected;
public static Texture2D OnNotSelected;
public static Texture2D OnSelected;
public static Texture2D FullScreen;
public static Texture2D Menu;
public static Texture2D Music;
public static Texture2D SliderBackground;
public static Texture2D Slider;
public static Texture2D SoundFX;
//End
#endregion
#region Load
public static void Load()
{
Background = Content.Load<Texture2D>(@"Images\StartUp\Background");
Continue = Content.Load<Texture2D>(@"Images\StartUp\Continue");
Credits = Content.Load<Texture2D>(@"Images\StartUp\Credits");
Exit = Content.Load<Texture2D>(@"Images\StartUp\Exit");
FullScreen = Content.Load<Texture2D>(@"Images\StartUp\FullScreen");
Logo = Content.Load<Texture2D>(@"Images\StartUp\Logo");
Menu = Content.Load<Texture2D>(@"Images\StartUp\Menu");
Music = Content.Load<Texture2D>(@"Images\StartUp\Music");
OffNotSelected = Content.Load<Texture2D>(@"Images\StartUp\OffNotSelected");
OffSelected = Content.Load<Texture2D>(@"Images\StartUp\OffSelected");
OnNotSelected = Content.Load<Texture2D>(@"Images\StartUp\OnNotSelected");
OnSelected = Content.Load<Texture2D>(@"Images\StartUp\OnSelected");
Options = Content.Load<Texture2D>(@"Images\StartUp\Options");
OptionsLogo = Content.Load<Texture2D>(@"Images\StartUp\OptionsLogo");
Play = Content.Load<Texture2D>(@"Images\StartUp\Play");
Slider = Content.Load<Texture2D>(@"Images\StartUp\Slider");
SliderBackground = Content.Load<Texture2D>(@"Images\StartUp\SliderBackground");
SoundFX = Content.Load<Texture2D>(@"Images\StartUp\SoundFX");
Version = Content.Load<Texture2D>(@"Images\StartUp\Version");
}
#endregion
#region Method
#endregion
}
}
I load in the Images here(in Game1.cs
):
protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
StartUp.Images.Load();
font1 = Content.Load<SpriteFont>(@"Fonts\Font1");
}
来源:https://stackoverflow.com/questions/20330785/screen-size-integers-c-sharp