1

私はゲームを作成しています。現時点では、greenpaddle、ball、および Game1 の 3 つのクラスがあります。

自分のゲームを実行すると、デバッガーが自分にホップダウンし、NullReferenceException UnhandledspriteBatch.Begin(); と表示されます。これは私の Game1.cs です:

public class Game1 : Microsoft.Xna.Framework.Game
{

    GraphicsDeviceManager graphics;
    SpriteBatch spriteBatch;
    Ball ball;
    GreenPaddle gPaddle;
    Texture2D BackGround;


    public Game1()
    {

        graphics = new GraphicsDeviceManager(this);
        Content.RootDirectory = "Content";
        graphics.PreferredBackBufferHeight = 500;
    }

    protected override void Initialize()
    {
        gPaddle = new GreenPaddle();
        ball = new Ball(gPaddle);
    }

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

        BackGround = Content.Load<Texture2D>("pongBG");
        gPaddle.LoadContent(Content);
        ball.LoadContent(Content);
    }

    /// <summary>
    /// UnloadContent will be called once per game and is the place to unload
    /// all content.
    /// </summary>
    protected override void UnloadContent()
    {
        // TODO: Unload any non ContentManager content here
    }

    /// <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)
    {
        // Allows the game to exit
        if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
            this.Exit();

        gPaddle.Update(gameTime);//Error Line
        ball.Update(gameTime);

        base.Update(gameTime);
    }

    /// <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)
    {
        GraphicsDevice.Clear(Color.CornflowerBlue);
        spriteBatch.Begin();//Error Line
        spriteBatch.Draw(BackGround, new Vector2(0f, 0f), Color.White);
        gPaddle.Draw(spriteBatch);
        ball.Draw(spriteBatch);
        spriteBatch.End();
        base.Draw(gameTime);
    }
}

何が悪いのかわからない、これは私に起こったことがない.

4

1 に答える 1