2013-07-27 2 views
1

나는 게임을 만들고 있으며, 현재 3 개의 클래스, greenpaddle, ball, Game1을 가지고 있습니다. spriteBatch가 Null입니다.

난 내 게임을 실행

는, 디버거 내 spriteBatch.Begin(); 까지 홉 NullReferenceException이 처리되지 않은는 말한다. 이 나에게 일어난 적이없는,
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); 
    } 
} 

잘못 알고하지 마십시오 이 내 내용은 Game1.cs입니다. 다른 클래스 중 하나가 변경되지 않는 한 그것은 null

답변

1

당신이 초기화되기 때문에의 SpriteBatch ...

spriteBatch = new SpriteBatch(GraphicsDevice);

.... 당신이 시도 할 수

것 : 부하의 콘텐츠에 중단 점 이것 저것 집어 넣어

, 나는 LoadContent()이되고 있는지 확인,이 호출 할 수 없습니다 이유를 알고 있지만 단지의 경우 선택하지 라는.

- 프로젝트를 작성하고 변경 사항이 저장되었는지 확인하십시오.


... 나는이 답변을 작성하고 컴퓨터에서 코드를 테스트하면서 마침내 오류를 발견했습니다. 다른 사람이 그 문제 중 하나를 가지고있는 경우에 대비하여 위의 조언을 남겨 둘 것입니다.

Initialize() 메서드에서 base.Initialize을 호출하지 않습니다. 이 메소드는 내부 XNA 항목을 호출하여 LoadContent()이 호출됩니다.

LoadContent() 메서드에서 base.LoadContent을 호출하는 것도 좋은 방법이므로 항상 재정의 된 메서드에서 기본 메서드를 호출해야합니다.

+0

알았어, 고마워, D –