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

Menu を使用して新しい Audio クラス インスタンスを追加すると、問題が発生します。Audio コンストラクターは新しい GameCompent を追加しようとしますが、失敗します。それが実際に行うことは、まったく新しい Game インスタンスを作成することです。次に、新しい Audio クラス インスタンスを追加しようとする新しい Menu をインスタンス化します。最終的に - を取得するまで、あなたは信じますか? - スタック オーバーフロー エラー。

私は何を間違っていますか?/ あるコンポーネントを別のコンポーネントから追加するにはどうすればよいですか?

4

1 に答える 1