1
public class Menu : DrawableGameComponent
{
    ContentManager Content;
    GraphicsDeviceManager graphics;
    SpriteBatch spriteBatch;

    Audio MenuMusic;

    public Menu(Game game) : base(game)
    {
        spriteBatch = Game.Services.GetService(typeof(SpriteBatch)) as SpriteBatch;
        graphics = Game.Services.GetService(typeof(GraphicsDeviceManager)) as GraphicsDeviceManager;

        Content = game.Content;
        Content.RootDirectory = @"Content\Menu\";

        *MenuMusic =  new Audio(game);* // Instantiate the new DrawableGameComponent

        Game.Components.Add(this);

        MenuMusic.PauseTune = false;
    }

    public override void Initialize()
    {
        Menustate = MenuState.LoadContent;
        base.Initialize();
    }

    protected override void LoadContent()
    {
         base.LoadContent();
    }

    protected override void UnloadContent()
    {
        base.UnloadContent();
    }

    public override void Update(GameTime gameTime)
    {
        base.Update(gameTime);
    }

    public override void Draw(GameTime gameTime)
    {
        base.Draw(gameTime);
    }
}

public class Audio : DrawableGameComponent
{

    public bool PauseTune
    {
        get { return PauseTune; }
        set { PauseTune = value; }
    }

    SoundEffect Tune = null;
    SoundEffectInstance SFXInstance;

    public Audio(Game game) : base(game)
    {
        *game.Components.Add(this)*;// This is the problem. It adds an entirely new Game object. :(
    }

    public override void Initialize()
    {
        PauseTune = true;

        base.Initialize();
    }

    protected override void LoadContent()
    {
        switch (Game1.Gamestate)
        {
            case GameState.Menu:
                string AudioPath = @"Audio\";
                Tune = Game.Content.Load<SoundEffect>(AudioPath + "Tune");
                break;
            case GameState.InitialiseGame:
                break;
            case GameState.PlayGame:
                break;
            default:
                break;
        }

        if (Tune != null) SFXInstance = Tune.CreateInstance();

        base.LoadContent();
    }

    protected override void UnloadContent()
    {
        base.UnloadContent();
    }

    public override void Update(GameTime gameTime)
    {
        if (Tune != null)
        {
            if (PauseTune)
            {
                if (SFXInstance.State == SoundState.Playing)
                    SFXInstance.Pause();
            }
            else
            {
                if (SFXInstance.State != SoundState.Playing)
                    SFXInstance.Play();
            }
        }
        base.Update(gameTime);
    }

    public override void Draw(GameTime gameTime)
    {
        base.Draw(gameTime);
    }
}

当我使用菜单添加一个新的音频类实例时,问题就来了。Audio 构造函数尝试添加新的 GameCompent 但失败。它实际上所做的是创建一个全新的 Game 实例,然后继续实例化一个新的 Menu 以尝试添加一个新的 Audio 类实例......直到我最终得到一个 - 你相信吗?- 堆栈溢出错误。

我究竟做错了什么?/ 如何从另一个组件中添加一个组件?

4

1 回答 1

1

哦!有些日子我永远不应该打开我的电脑:)

或者更好 - 经过漫长而疲惫的一天后,不要乱用 mycode。

自引用 PauseTune 属性。

于 2015-12-11T11:33:47.440 回答