所以我试图在学校的Visual Studio 2012中创建这个完美的程序。
但是当我在家时它会在Public Game1()
给我这个抛出异常错误。
希望有人能帮助我解决这个问题,这样我才能真正运行我的程序并完成它。
调用堆栈是:
System.NullReferenceException:未将对象引用设置为实例 Microsoft.Xna.Framework.OpenTKGameWindow.Initialize中的对象 ()[0x00000] in:0 at Microsoft.Xna.Framework.OpenTKGameWindow..ctor()[0x00000] in :0点 Microsoft.Xna.Framework.OpenTKGamePlatform..ctor (Microsoft.Xna.Framework.Game游戏)[0x00000] in:0 在Microsoft.Xna.Framework.GamePlatform.Create (Microsoft.Xna.Framework.Game游戏)[0x00000] in:0 在Microsoft.Xna.Framework.Game..ctor()[0x00000] in:0,在MonoGameTest.Game1..ctor()[0x00023] in /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>(@"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:无法加载 徽标资产作为非内容文件! ---&GT; System.Exception: 目录未找到。 ---&GT; 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] 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(System.String路径,FileMode模式, FileAccess访问,FileShare共享)[0x00000] 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:132 at at(wrapper remoting-invoke-with-check)System.IO.FileStream:.ctor (字符串,System.IO.FileMode,System.IO.FileAccess,System.IO.FileShare)
在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 name)[0x00000] in:0 at Microsoft.Xna.Framework.Content.ContentManager.OpenStream (System.String assetName)[0x00000] in:0 --- End 内部异常堆栈跟踪--- at Microsoft.Xna.Framework.Content.ContentManager.OpenStream (System.String assetName)[0x00000] in:0 at 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)&lt; 0x0076b&gt; at at Microsoft.Xna.Framework.Content.ContentManager.Load (字符串)&lt; 0x0020b&gt;在BSMac.Game1.LoadContent()[0x00023]中 /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()
在Microsoft.Xna.Framework.Game.Run (Microsoft.Xna.Framework.GameRunBehavior)at 在Microsoft.Xna.Framework.Game.Run()中 BSMac.AppDelegate.FinishedLaunching(MonoMac.Foundation.NSObject) [0x00012]在/Users/sebnabt/Projects/BSMac/BSMac/Main.cs:34 at at (包装器动态方法)对象。[BSMac.AppDelegate.Void FinishedLaunching(MonoMac.Foundation.NSObject)] (MonoMac.Foundation.NSObject,MonoMac.ObjCRuntime.Selector,MonoMac.Foundation.NSObject) 在at(包装本地到管理) 对象。[BSMac.AppDelegate.Void FinishedLaunching(MonoMac.Foundation.NSObject)] (MonoMac.Foundation.NSObject,MonoMac.ObjCRuntime.Selector,MonoMac.Foundation.NSObject) at at(包装器管理到原生) MonoMac.AppKit.NSApplication.NSApplicationMain(int,string []) &LT; 0x00003&GT;在BSMac.Program.Main(string [])[0x0001d]的MonoMac.AppKit.NSApplication.Main(string [])中 /Users/sebnabt/Projects/BSMac/BSMac/Main.cs:20 at
答案 0 :(得分:0)
你在Xamarin工作室建造吗?我相信您在几天前遇到的问题中描述的问题相同;在那里看到我的答案:https://stackoverflow.com/a/18798311/793449
总结一下,Xamarin Studio正在选择WindowsGL框架程序集而不是mac程序集。您应该从最新的源代码(MonoGame.Framework.MacOS项目)自己构建MonoGame,并在游戏中更改引用。