StackoverflowException未处理 - 了解原因,不知道如何修复

时间:2014-03-23 13:23:35

标签: c# xna

我目前为我的游戏提供了3个.cs文件,这些文件是Game1.cs(主文件),Player.cs和HUD.cs,目前我被困在一个循环中,Game1.cs类创建了一个新的HUD对象这将创建一个新的Game1对象等。下面的代码(使用XNA框架btw工作)。

HUD.cs

public class HUD
{
    public Texture2D HUD_main, HUD_stamina, HUD_sBarDepleted, HUD_mana, HUD_health, HUD_money; //HUD TEXTURES
    public int staminaWidth, manaWidth;
    public SpriteFont HUDFont;
    Player_Main pmain = new Player_Main();
    Game1 gamecore = new Game1();

    public HUD()
    {

    }
    public void Update(GameTime gameTime)
    {
        staminaWidth = (int)pmain.pstats.currentStamina * 2; //Updating width of stamina bar based off of current stamina
        pmain.pstats.staminaDisplay = Math.Round(pmain.pstats.currentStamina); //Separate variable for the stamina that shows on screen so it doesn't print decimals

        manaWidth = (int)pmain.pstats.currentMana;
        pmain.pstats.manaDisplay = Math.Round(pmain.pstats.currentMana);
    }
    public void LoadContent(ContentManager Content)
    {
        HUD_main = Content.Load<Texture2D>("HUD/HUD");
        HUD_stamina = Content.Load<Texture2D>("HUD/staminabar");
        HUDFont = Content.Load<SpriteFont>("HUD/HUDFont");
        HUD_sBarDepleted = Content.Load<Texture2D>("HUD/bar_depleted");
        HUD_mana = Content.Load<Texture2D>("HUD/manabar");
        HUD_money = Content.Load<Texture2D>("HUD/moneybar");
        HUD_health = Content.Load<Texture2D>("HUD/healthbar");
    }
    public void Draw(SpriteBatch spriteBatch)
    {
        spriteBatch.Draw(HUD_main, new Rectangle(0, 0, gamecore.viewportWidth, gamecore.viewportHeight), Color.White);
        spriteBatch.Draw(HUD_sBarDepleted, new Rectangle(100, 0, 200, 20), new Rectangle(0, 0, 200, 20), Color.White);
        spriteBatch.Draw(HUD_stamina, new Rectangle(100, 0, staminaWidth, 20), new Rectangle(0, 0, staminaWidth, 20), Color.White);
        spriteBatch.Draw(HUD_sBarDepleted, new Rectangle(100, 20, 200, 20), new Rectangle(0, 0, 200, 20), Color.White);
        spriteBatch.Draw(HUD_mana, new Rectangle(100, 20, manaWidth, 20), new Rectangle(0, 0, manaWidth, 20), Color.White);
        spriteBatch.DrawString(HUDFont, pmain.pstats.staminaDisplay + "/" + pmain.pstats.maxStamina, new Vector2(175, 0), Color.White);
        spriteBatch.DrawString(HUDFont, pmain.pstats.manaDisplay + "/" + pmain.pstats.maxMana, new Vector2(175, 20), Color.White);
        spriteBatch.DrawString(HUDFont, "Ability: Cooldown", new Vector2(20, 40), Color.White);
        spriteBatch.DrawString(HUDFont, "Teleport: " + pmain.pabilities.spellDisplay1 + "s", new Vector2(20, 60), Color.White);
    }
}

Game1.cs

public class Game1 : Microsoft.Xna.Framework.Game
{
    GraphicsDeviceManager graphics;
    SpriteBatch spriteBatch;
    Player_Main player = new Player_Main();
    HUD HUDmain = new HUD();
    Texture2D grass1; //MAP TEXTURES

    public int viewportWidth, viewportHeight;


    //Classes

    public Game1()
    {
        graphics = new GraphicsDeviceManager(this);
        graphics.PreferredBackBufferWidth = 1280;
        graphics.PreferredBackBufferHeight = 720;
        Content.RootDirectory = "Content";
        this.Window.Title = "Honour In Blood: Rise Against Rome";

    }

    /// <summary>
    /// Allows the game to perform any initialization it needs to before starting to run.
    /// This is where it can query for any required services and load any non-graphic
    /// related content.  Calling base.Initialize will enumerate through any components
    /// and initialize them as well.
    /// </summary>
    protected override void Initialize()
    {
        base.Initialize();
    }

    /// <summary>
    /// LoadContent will be called once per game and is the place to load
    /// all of your content.
    /// </summary>
    protected override void LoadContent()
    {
        // Create a new SpriteBatch, which can be used to draw textures.

        spriteBatch = new SpriteBatch(GraphicsDevice);
        grass1 = Content.Load<Texture2D>("grass1");

        viewportHeight = GraphicsDevice.Viewport.Height;
        viewportWidth = GraphicsDevice.Viewport.Width;
        player.LoadContent(Content);
        HUDmain.LoadContent(Content);

    }


    protected override void UnloadContent()
    {
    }

    /// <summary>
    /// Allows the game to run logic such as updating the world,
    /// checking for collisions, gathering input, and playing audio.
    /// </summary>
    protected override void Update(GameTime gameTime)
    {
        KeyboardState KS = Keyboard.GetState();
        // Allows the game to exit
        if (KS.IsKeyDown(Keys.Escape))
            this.Exit();



        //Making edge of screen stop player movement to prevent going off-screen
        if (player.pmove.position.X < 0)
            player.pmove.position.X = 0;
        if (player.pmove.position.Y < 0)
            player.pmove.position.Y = 0;
        if (player.pmove.position.X + player.psprite.player.Width > GraphicsDevice.Viewport.Width)
            player.pmove.position.X = GraphicsDevice.Viewport.Width - player.psprite.player.Width;
        if (player.pmove.position.Y + player.psprite.player.Height > GraphicsDevice.Viewport.Height)
            player.pmove.position.Y = GraphicsDevice.Viewport.Height - player.psprite.player.Height;
        //



        player.Update(gameTime);
        HUDmain.Update(gameTime);
        //
        base.Update(gameTime);
    }

    /// <summary>
    /// This is called when the game should draw itself.
    /// </summary>
    protected override void Draw(GameTime gameTime)
    {
        GraphicsDevice.Clear(Color.CornflowerBlue);
        KeyboardState KS = Keyboard.GetState();

        spriteBatch.Begin(SpriteSortMode.Deferred, null, SamplerState.LinearWrap, null, null); // TILING
        spriteBatch.Draw(grass1, new Rectangle(0, 0, viewportWidth, viewportHeight),new Rectangle(0,0,viewportWidth,viewportHeight), Color.White); //TILES BACKGROUND TO RESOLUTION OF WINDOW
        player.Draw(spriteBatch);
        HUDmain.Draw(spriteBatch);
        spriteBatch.End();

        base.Draw(gameTime);
    }
}

如何在我的hud类中使用game1.cs中的变量而不必创建新对象?

1 个答案:

答案 0 :(得分:0)

您帖子的第一条评论解释了您的StackOverflowException。

将对Game实例的引用传递给Hud组件,然后可以引用它及其属性。这就是Microsoft.Xna.Framework.GameComponent的设计方式。

在Hud.cs中:

Game1 gamecore; 

public HUD(Game1 game)
{
    gamecore = game;
}

在Game1.cs中

HUD HUDmain;

public Game1()
    {
        graphics = new GraphicsDeviceManager(this);
        graphics.PreferredBackBufferWidth = 1280;
        graphics.PreferredBackBufferHeight = 720;
        Content.RootDirectory = "Content";
        this.Window.Title = "Honour In Blood: Rise Against Rome";

        HUDmain = new HUD(this);
    }