Entwickler-Ecke

Multimedia / Grafik - XNA 4.0 Exception "GraphicsDevice component not found."


C# - Mo 02.01.12 18:55
Titel: XNA 4.0 Exception "GraphicsDevice component not found."
Hallo,
ich bin vor kurzem auf XNA gestoßen - bin also Neuling. Wenn ich jetzt mein Game Lade, kommt beim Laden einer SpriteFont die oben genannte Exception.
Ich Lade diese Font außerhalb der LoadContent Methode. Genauergesagt in einer anderen Klasse.
Hier der Code:

C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
namespace xyz
{
    public partial class Game1 : Game
    {
  //Variablendeklaration...

        protected GraphicsDeviceManager graphics;
        protected SpriteBatch spriteBatch;

        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
        }

        protected override void LoadContent()
        {         
            Video video = Content.Load<Video>("Intro");
            videoPlayer.Play(video);
            GameMode = GameMode.None;
            font = Content.Load<SpriteFont>("Courier New");
            spriteBatch = new SpriteBatch(GraphicsDevice);
            player.Texture = Content.Load<Texture2D>("breaker");
            player.Speed = SCREENWIDTH / 10;
            Menu.Load();
            GameState = GameState.Loaded;
        }
    }
}


C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
namespace xyz
{
    public class Menu : Game1
    {        

  //...
  public void Load()
        {
            StringStartPosition = WINDOW.Height / 5;
            StringDistance = WINDOW.Height / 10;
            Font = Content.Load<SpriteFont>("MenuFont"); //Hier tritt die Exception auf
            Strings.Add(new SelectableString("You vs the Clock", Font, "Reach the goal as fast as you can. The clock is ticking!"));
            Strings.Add(new SelectableString("Apocalyptic Mode", Font, "Reach the goal as fast as you can. You have weapons and other usefull things. Use them! The clock is ticking!"));
            Strings.Add(new SelectableString("Create Map", Font, "Create your own maps and play them."));
            Strings.Add(new SelectableString("Options", Font, "Change the settings of the game"));
            Strings.Add(new SelectableString("Exit", Font, "Leave the game. Are you sure?"));
            Background = Content.Load<Texture2D>("Menu");
            Color = Set.MenuColor;
            SelectionColor = Set.MenuSelectedColor;
            Strings[0].Select = true;
            SelectedString = 0;
            InfoText = Strings[0].TextInfo;
        }
  //...
    }
}


Weiß jemand wie ich das beheben kann?


Th69 - Mo 02.01.12 19:38

Hallo C#,

mir erscheint dein Klassendesign etwas eigenartig zu sein: du leitest Menu von Game1 ab, so daß es also selber vom Typ Game ist. Ich denka aber, daß es in XNA nur genau eine Game-Klasse (bzw. Instanz) geben sollte.

Außerdem hast du schon mal im Internet nach deiner Fehlermeldung geschaut, z.B.
http://stackoverflow.com/questions/3893796/player-class-with-my-c-sharp-xna-game
http://forums.create.msdn.com/forums/t/26469.aspx
http://www.xnamag.de/forum/viewtopic.php?p=34432


C# - Mo 02.01.12 23:45

Danke. Ich habe das Problem jetzt gelöst. Ich habe über eine weitere, statische Klasse diw Wichtigsten Objekte aufgelistet. So habe ich auch einen Zeiger auf meine Game1-Klasse. Über diesen Zeiger kann ich problemlos die Contents laden.