0

所以我正在尝试创建这个在学校的 Visual Studio 2012 中完美运行的程序。

但是当我在家的时候,它给了我这个 Thrown Exception 错误Public Game1()

希望有人可以帮助我解决这个问题,这样我就可以真正运行我的程序并完成它。

调用堆栈是:

System.NullReferenceException:对象引用未设置为 Microsoft.Xna.Framework.OpenTKGameWindow.Initialize () [0x00000] 中的对象实例:0 在 Microsoft.Xna.Framework.OpenTKGameWindow..ctor () [0x00000] 中: 0 在 Microsoft.Xna.Framework.OpenTKGamePlatform..ctor(Microsoft.Xna.Framework.Game 游戏)[0x00000] 在:0 在 Microsoft.Xna.Framework.GamePlatform.Create(Microsoft.Xna.Framework.Game 游戏)[0x00000 ] 在:0 在 Microsoft.Xna.Framework.Game..ctor () [0x00000] 在:0 在 MonoGameTest.Game1..ctor () [0x00023] 在 /Users/sebnabt/Projects/MonoGameTest/MonoGameTest/Game1.cs :38 在 MonoGameTest.Program.Main () [0x00001] 在 /Users/sebnabt/Projects/MonoGameTest/MonoGameTest/Program.cs:19

我的代码:

<!-- language: lang-cs -->

region Using Statements
using System; 
using System.Collections.Generic; 
using System.Linq; 

using Microsoft.Xna.Framework; 
using Microsoft.Xna.Framework.Graphics; 
using Microsoft.Xna.Framework.Storage; 
using Microsoft.Xna.Framework.Input;
endregion

namespace MonoGameTest 
{
/// <summary>
/// This is the main type for your game
/// </summary>
public class Game1 : Game
{
 GraphicsDeviceManager graphics;
 SpriteBatch spriteBatch;

 // Maps user keys to display colors
 Dictionary<Keys, Color> key_to_color = new Dictionary<Keys, Color>();

 // these are the colors guessed by the user, so far.
 List<Color>[] player_colors = new List<Color>[10];

 // these are the keys that correspond to a dot color
 List<Keys> color_letters = new List<Keys>();

 Texture2D dot_picture;

 KeyboardState old_keyboard_state;

 int row;        

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

     base.Initialize ();
 } 

 /// <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>
 /// 
 /// // screen constants
 const int SCREEN_WIDTH = 640;
 const int SCREEN_HEIGHT = 480;


 protected override void Initialize()
 {
     row = 0;
     // back buffer
     graphics.PreferredBackBufferHeight = SCREEN_HEIGHT;
     graphics.PreferredBackBufferWidth = SCREEN_WIDTH;
     graphics.PreferMultiSampling = false;
     graphics.ApplyChanges();
     base.Initialize();

     // initialize the color dictionary
     key_to_color.Add(Keys.R, Color.Red);
     key_to_color.Add(Keys.G, Color.Green);
     key_to_color.Add(Keys.B, Color.Blue);
     key_to_color.Add(Keys.Y, Color.Yellow);

     color_letters = new List<Keys>();
     foreach (Keys letter in key_to_color.Keys)
     {
        color_letters.Add(letter);
     }

  }

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

    // load our textures
    dot_picture = Content.Load<Texture2D>(@"media\ball");
  }

  /// <summary>
  /// Allows the game to run logic such as updating the world,
  /// checking for collisions, gathering input, and playing audio.
  /// </summary>
  /// <param name="gameTime">Provides a snapshot of timing values.</param>
  protected override void Update(GameTime gameTime)
  {
    // get keyboard state once per frame
    KeyboardState keyState = Keyboard.GetState();

    // exit the game?
    if (keyState.IsKeyDown(Keys.Escape))
    {
        this.Exit();
    }

    // erase list of player colors?
    else if (keyState.IsKeyDown(Keys.Space))
    {
        player_colors[this.row].Clear();
    }

    // player pressed a color key?
    else {
        foreach (Keys letter in color_letters)
        {
            if (key_was_released(letter, keyState))
            {
                add_color(letter);  
            }
            if (this.player_colors [row].Count () >= 4) {
                this.row = this.row + 1;
            }
        }
    }



    old_keyboard_state = keyState;

    base.Update(gameTime);
}

protected override void UnloadContent() {

}

bool key_was_released(Keys key, KeyboardState new_state)
{
    return old_keyboard_state.IsKeyDown(key) && 
        new_state.         IsKeyUp  (key);
}

void add_color(Keys key)
{
    Color color = key_to_color[key];
    if (player_colors[this.row].Count() < 4)
    {
        player_colors[this.row].Add(color);
    }
}
/// <summary>
/// This is called when the game should draw itself.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
protected override void Draw(GameTime gameTime)
{
    graphics.GraphicsDevice.Clear (Color.CornflowerBlue);

    // draw typical sprites
    spriteBatch.Begin ();
    draw_player_colors (spriteBatch);
    spriteBatch.End ();

    base.Draw (gameTime);
}

protected void draw_player_colors(SpriteBatch batch)
{
    for (int current_row = 0; current_row < 10; current_row++) {
        for (int column = 0; column < player_colors[current_row].Count(); column++) {
            int x = 100 + column * 70;
            int y = 100 + column * 70;
            Vector2 loc = new Vector2 (x, y);
            Color c = player_colors[current_row].ElementAt (column);
            batch.Draw (dot_picture, loc, c);
        }
    }
}
}

}

最新错误:

看起来好像我已经让它稍微工作了但是我现在在基本模板中遇到错误:logoTexture = Content.Load<Texture2D>("logo");

错误将在下面

Microsoft.Xna.Framework.Content.ContentLoadException:无法将徽标资产加载为非内容文件!---> System.Exception:找不到目录。---> System.Exception:找不到路径“/Users/sebnabt/Projects/BSMac/BSMac/bin/Debug/BSMac.app/Contents/Resources/Content/logo.xnb”的一部分。在 System.IO.FileStream..ctor(System.String 路径、FileMode 模式、FileAccess 访问、FileShare 共享、Int32 bufferSize、布尔匿名、FileOptions 选项)[0x001c6] 在 /private/tmp/source/bockbuild-xamarin/profiles/ Mono-mac-xamarin-no-pcl/build-root/mono-3.2.0/mcs/class/corlib/System.IO/FileStream.cs:266 在 System.IO.FileStream..ctor (System.String 路径, FileMode 模式,FileAccess 访问,
在 System.IO.File.OpenRead (System.String path) [0x00000] 在 /private/tmp/source/bockbuild-xamarin/profiles/mono-mac-xamarin-no-pcl/build-root/mono-3.2.0 /mcs/class/corlib/System.IO/File.cs:341 在 Microsoft.Xna.Framework.TitleContainer.OpenStream (System.String 名称) [0x00000] 在 :0 在 Microsoft.Xna.Framework.Content.ContentManager.OpenStream (System.String assetName) [0x00000] in :0 --- End of internal exception stack trace --- at Microsoft.Xna.Framework.Content.ContentManager.OpenStream (System.String assetName) [0x00000] in :0 at Microsoft .Xna.Framework.Content.ContentManager.ReadAsset[Texture2D] (System.StringassetName, System.Action1 recordDisposableObject) [0x00000] in <filename unknown>:0 --- End of inner exception stack trace --- at at Microsoft.Xna.Framework.Content.ContentManager.ReadAsset<Microsoft.Xna.Framework.Graphics.Texture2D> (string,System.Action1) <0x0076b> at Microsoft.Xna.Framework.Content.ContentManager.Load (string) <0x0020b> at BSMac.Game1.LoadContent () [0x00023] in /Users/sebnabt/Projects/BSMac/BSMac/Game1.cs :70 at Microsoft.Xna.Framework.Game.Initialize () at BSMac.Game1.Initialize () [0x00002] in /Users/sebnabt/Projects/BSMac/BSMac/Game1.cs:57 at Microsoft.Xna.Framework .Game.DoInitialize()
在 Microsoft.Xna.Framework.Game.Run (Microsoft.Xna.Framework.GameRunBehavior) 在 Microsoft.Xna.Framework.Game.Run() 在 BSMac.AppDelegate.FinishedLaunching (MonoMac.Foundation.NSObject) [0x00012] 中/Users/sebnabt/Projects/BSMac/BSMac/Main.cs:34 at (wrapper dynamic-method) object.[BSMac.AppDelegate.Void FinishedLaunching(MonoMac.Foundation.NSObject)] (MonoMac.Foundation.NSObject,MonoMac。 ObjCRuntime.Selector,MonoMac.Foundation.NSObject) 在(包装器本机到托管)对象。[BSMac.AppDelegate.Void FinishedLaunching(MonoMac.Foundation.NSObject)] (MonoMac.Foundation.NSObject,MonoMac.ObjCRuntime.Selector, MonoMac.Foundation.NSObject) 在 (wrapper managed-to-native) MonoMac.AppKit.NSApplication.NSApplicationMain (int,string[]) <0x00003> 在 MonoMac.AppKit.NSApplication。Main (string[]) at BSMac.Program.Main (string[]) [0x0001d] in /Users/sebnabt/Projects/BSMac/BSMac/Main.cs:20 at

4

1 回答 1

0

你在 Xamarin Studio 中构建吗?我相信您遇到了几天前的问题中描述的相同问题;在那里看到我的答案:https ://stackoverflow.com/a/18798311/793449

回顾一下,Xamarin Studio 选择的是 WindowsGL 框架程序集而不是 mac 程序集。您应该从最新的源代码(MonoGame.Framework.MacOS 项目)自己构建 MonoGame,并在游戏中更改引用。

于 2013-09-14T04:37:34.823 回答