所以我正在尝试创建这个在学校的Visual Studio 2012中完美运行的程序。
但是当我在家时,它会在Public Game1()
给我这个抛出异常错误。
希望有人可以帮助我解决这个问题,这样我就可以实际运行我的程序并完成它。
调用堆栈为:
System.NullReferenceException:对象引用未设置为实例 在 Microsoft.Xna.Framework.OpenTKGameWindow.Initialize () [0x00000] 在 :0 在 Microsoft.Xna.Framework.OpenTKGameWindow..克托尔 () [0x00000] 在 :0 在 Microsoft.Xna.Framework.OpenTKGamePlatform..克托尔 (Microsoft.Xna.Framework.Game game)[0x00000] in :0 at Microsoft.Xna.Framework.GamePlatform.Create (Microsoft.Xna.Framework.Game game)[0x00000] in :0 在Microsoft.Xna.Framework.Game..ctor () [0x00000] in :0 在MonoGameTest.Game1..克托尔 () [0x00023] 在 /Users/sebnabt/Projects/MonoGameTest/MonoGameTest/Game1.cs:38 at MonoGameTest.Program.Main () [0x00001] in /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>(@"mediaball");
}
/// <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:无法加载 徽标资产作为非内容文件!--->系统异常: 找不到目录。--->系统异常:找不到 路径的一部分 "/Users/sebnabt/Projects/BSMac/BSMac/bin/Debug/BSMac.app/Content/Resources/Content/logo.xnb"。 在System.IO.FileStream..ctor (系统字符串路径, 文件模式, 文件访问访问、文件共享共享、Int32 缓冲区大小、布尔值 匿名,文件选项选项)[0x001c6] in /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 (系统字符串路径, 文件模式, 文件访问访问,文件共享共享) [0x00000] 在 /private/tmp/source/bockbuild-xamarin/profiles/mono-mac-xamarin-no-pcl/build-root/mono-3.2.0/mcs/class/corlib/System.IO/FileStream.cs:132 at at (wrapper remoting-invoke-with-check) System.IO.FileStream:.ctor (string,System.IO.FileMode,System.IO.FileAccess,System.IO.FileShare)
at System.IO.File.OpenRead (System.String path) [0x00000] in /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 at Microsoft.Xna.Framework.TitleContainer.OpenStream (System.String 名称) [0x00000] 在 :0 中 在 Microsoft.Xna.Framework.Content.ContentManager.OpenStream (系统字符串资产名称)[0x00000] in :0 --- 结束 的内部异常堆栈跟踪--- Microsoft.Xna.Framework.Content.ContentManager.OpenStream (系统字符串资产名称)[0x00000] in :0 在 Microsoft.Xna.Framework.Content.ContentManager.ReadAsset[Texture2D] (System.String assetName, 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.Action
1) <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 at Microsoft.Xna.Framework.Game.Initialize () at BSMac.Game1.Initialize () [0x00002] in /Users/sebnabt/Projects/BSMac/BSMac/Game1.cs:57 at at Microsoft.Xna.Framework.Game.DoInitialize ()
at at Microsoft.Xna.Framework.Game.Run (Microsoft.Xna.Framework.GameRunBehavior) at at Microsoft.Xna.Framework.Game.Run () at BSMac.AppDelegate.DoneLaunch (MonoMac.Foundation.NSObject) [0x00012] in/Users/sebnabt/Projects/BSMac/BSMac/Main.cs:34 at at (包装器动态方法)对象。[BSMac.AppDelegate.Void FinishLaunching(MonoMac.Foundation.NSObject)] (MonoMac.Foundation.NSObject,MonoMac.ObjCRuntime.Selector,MonoMac.Foundation.NSObject) at at (包装器本机到托管) 对象。[BSMac.AppDelegate.Void FinishLaunching(MonoMac.Foundation.NSObject)] (MonoMac.Foundation.NSObject,MonoMac.ObjCRuntime.Selector,MonoMac.Foundation.NSObject) at at (包装器托管到本机) MonoMac.AppKit.NSApplication.NSApplicationMain (int,string[]) <0x00003> at at MonoMac.AppKit.NSApplication.Main (string[]) at BSMac.Program.Main (string[]) [0x0001d] in /Users/sebnabt/Projects/BSMac/BSMac/Main.cs:20 at
你在 Xamarin Studio 中构建吗? 我相信您遇到了几天前问题中描述的相同问题;看到我的答案:https://stackoverflow.com/a/18798311/793449
回顾一下,Xamarin Studio 正在选择 WindowsGL 框架程序集而不是 mac 程序集。 您应该从最新的源代码(MonoGame.Framework.MacOS项目)自己构建MonoGame,并在游戏中更改引用。