屏幕大小整数。 C#

时间:2013-12-02 14:34:39

标签: c# visual-studio-2010 methods xna int

我创建了一个类Resize.cs,它从Images.cs类中获取图像,并通过为每个宽度和高度创建一个新的整数来查找图像的宽度和高度。我还调整大小以改变图像的大小以适应屏幕。

public static int resize = 1;

public static int backgroundWidth = resize * Images.Background.Width;
public static int backgroundHeight = resize * Images.Background.Height;

然后我取宽度和高度并将其移到Game1.cs(主类)和public Game1()我改变屏幕的宽度和高度,这有效:

    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;
    }

要:

    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;
    }

但无论我做什么,Program.cs(我从未接触过)都会出现错误:(这只会在我启动程序时发生,它会说它很好,并且在程序启动后结束)

TypeInitializationException未处理  “Love__Regret.Game1”的类型初始化程序引发了异常。

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
}

最后我的目标是在程序内随意调整屏幕大小。 (因为如果2台具有不同屏幕大小的计算机使用该程序,它应该在两者中正确匹配)

我插入常规数字高度= 100和宽度= 157,它们工作正常,并按照应有的大小调整屏幕大小。

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
    }
}

我在这里加载图片(在Game1.cs中):

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

            StartUp.Images.Load();
            font1 = Content.Load<SpriteFont>(@"Fonts\Font1");
        }

0 个答案:

没有答案