XNAに問題があります。私は伸び縮みによって "呼吸"するテキストを得ようとしていますが、正しく動かないでしょう。コンパイルは正常ですが、 "NullReferenceException:オブジェクト参照がオブジェクトのインスタンスに設定されていません。"一度実行するとエラーになります。 Game1.csとBreathingText.csの2つのファイルがあります。XNAのNull参照例外
BreathingText.cs:
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
namespace TestingThings
{
class BreathingText
{
public void drawText(string textToDraw, int X, int Y)
{
int fontRestraint = 1;
Game1 game = new Game1();
GraphicsDeviceManager graphics = game.graphics;
SpriteBatch sB = game.spriteBatch;
SpriteFont font = game.gameFont;
Vector2 FontPos = new Vector2(X, Y);
Vector2 StrCenter = new Vector2(0,0); //font.MeasureString(textToDraw)/2;
sB.Begin();
sB.DrawString(font,
textToDraw,
FontPos,
Color.LightGreen,
0,
StrCenter,
1.0f,
SpriteEffects.None,
0.5f);
if (fontRestraint == 1)
{
font.Spacing += 0.25F;
}
else if (fontRestraint == 0)
{
font.Spacing -= 0.25F;
}
if (font.Spacing == 17)
{
fontRestraint = 0;
}
else if (font.Spacing == 0)
{
fontRestraint = 1;
}
sB.End();
}
}
}
例外がsB.Begin()
で起こります。私はGame1.csのspriteBatch
が初期化されていなかったので起きていると思っていましたが、それは私のように見えるはずです。ここGame1.csです:
namespace TestingThings
{
public class Game1 : Microsoft.Xna.Framework.Game
{
public GraphicsDeviceManager graphics;
public SpriteBatch spriteBatch;
public SpriteFont gameFont;
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}
protected override void Initialize()
{
base.Initialize();
}
protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
gameFont = Content.Load<SpriteFont>("Celestia");
}
protected override void Update(GameTime gameTime)
{
// Allows the game to exit
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
{ this.Exit(); }
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.Black);
BreathingText breath = new BreathingText();
breath.drawText("test", graphics.GraphicsDevice.Viewport.Width/2, graphics.GraphicsDevice.Viewport.Height - 25);
base.Draw(gameTime);
}
}
}
それが必要のように、それはそうなので、私は、なぜそれが働いていないか分かりません。しかし、私は初心者であり、ソリューションは私の鼻の下にある可能性が最も高いです。 Game1のDrawメソッドにBreathingText.csコードがあり、それを自分のクラスに移動するまでうまくいきました。
ご協力いただければ幸いです。そのゲームのspritebatchに値が割り当てられることはありませんので、
おかげで、 MrAnthology
コードまたはXNAコードでヌル参照例外が発生していますか? –
私自身のBreathingText.csの 'sB.Begin()'で起きているので、私はそれが自分自身のことだと仮定しています。私が言ったように、私はC#を初めて使っているので、このようなことをデバッグする方法はまだ学んでいます。 – MrAnthology
私はXNAを使用しませんが、 'Game1.LoadContent'が基本クラスのコンストラクタで呼び出されていないようです。これはいつ呼び出されるべきですか? – Groo