2014-11-13 2 views
-1

그래서 문제가 생겼습니다. drawablegamecomponent 클래스의 메인 Game1 클래스에 객체를 그립니다.XNA 객체가 그리지 않습니다

나는 조금 곁에서 놀고 다른 예제를보고 있지만 문제를 파악할 수는 없습니다. 많은 코드가있는 것은 아니므로, 제가 가지고있는 두 클래스 인 Game1과 내가 그리려는 클래스 인 Balloon을 게시 할 것입니다.

오류가 ...이 얻을 추첨 방법에

An unhandled exception of type 'System.NullReferenceException' occurred in Burst.exe 

Additional information: Object reference not set to an instance of an object. 

메인 클래스입니다.

using System; 
using System.Collections.Generic; 
using System.Linq; 
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 Burst 
{ 
    /// <summary> 
    /// This is the main type for your game 
    /// </summary> 
    public class Game1 : Microsoft.Xna.Framework.Game 
    { 
     GraphicsDeviceManager graphics; 
     SpriteBatch spriteBatch; 
     Rectangle bounds; 
     Balloon ball; 
     Diamond dia; 




     public Game1() 
     { 
      graphics = new GraphicsDeviceManager(this); 
      graphics.PreferredBackBufferWidth = 1152; 
      graphics.PreferredBackBufferHeight = 648; 
      Content.RootDirectory = "Content"; 


      bounds = new Rectangle(0, 0, graphics.PreferredBackBufferWidth, graphics.PreferredBackBufferHeight); 

      ball = new Balloon(bounds, 1, this, spriteBatch); 
      dia = new Diamond(bounds, new Vector2(200, 200), this, spriteBatch); 

     } 




     /// <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> 
     protected override void Initialize() 
     { 
      // TODO: Add your initialization logic here 


      base.Initialize(); 
     } 

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



      // TODO: use this.Content to load your game content here 
     } 

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

      // TODO: Add your update logic here 

      ball.Update(); 
      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(); 
      // TODO: Add your drawing code here 
      ball.Draw(gameTime); 
      dia.Draw(gameTime); 
      spriteBatch.End(); 
      base.Draw(gameTime); 
     } 
    } 
} 

풍선 클래스 :이 객체 "볼"과 "디아"를 만들

using Microsoft.Xna.Framework; 
using Microsoft.Xna.Framework.Graphics; 
using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 

namespace Burst 
{ 

    public class Balloon : Microsoft.Xna.Framework.DrawableGameComponent 
    { 

     Vector2 position; 
     Vector2 motion; 
     Texture2D texture; 
     Rectangle bounds; 
     Rectangle screenBounds; 
     Game game; 
     SpriteBatch spriteBatch; 


     float balloonSpeed = 4; 

     int colour = 0; 




     public Balloon(Rectangle screenBounds, int colour, Game game, SpriteBatch spriteBatch) : base (game) 
     { 
      this.colour = colour; 
      this.game = game; 
      this.spriteBatch = spriteBatch; 
      this.screenBounds = screenBounds; 
      this.position = new Vector2(200,200); 

     } 

     protected override void LoadContent() 
     { 

      switch (colour) 
      { 
       case 1: texture = Game.Content.Load<Texture2D>("Images/BlueBalloon"); 
        break; 
       case 2: texture = Game.Content.Load<Texture2D>("Images/GreenBalloon"); 
        break; 
       case 3: texture = Game.Content.Load<Texture2D>("Images/RedBalloon"); 
        break; 
       case 4: texture = Game.Content.Load<Texture2D>("Images/YellowBalloon"); 
        break; 
       case 5: texture = Game.Content.Load<Texture2D>("Images/PurpleBalloon"); 
        break; 
      } 
     } 


     public Rectangle Bounds 
     { 
      get 
      { 
       bounds.X = (int)position.X; 
       bounds.Y = (int)position.Y; 
       return bounds; 
      } 
     } 


     public void Update() 
     { 
      position += motion * balloonSpeed; 

     } 

     private void CheckWallColision() 
     { 
      if (position.X < 0) 
      { 
       position.X = 0; 
       motion.X *= -1; 
      } 

      if (position.X + texture.Width > screenBounds.Width) 
      { 
       position.X = screenBounds.Width - texture.Width; 
       motion.X *= -1; 
      } 

      if (position.Y < 0) 
      { 
       position.Y = 0; 
       motion.Y *= -1; 
      } 

      if (position.Y + texture.Height > screenBounds.Height) 
      { 

       position.Y = screenBounds.Height - texture.Height; 
       motion.Y *= -1; 
      } 
     } 

     public void SetStartPosition() 
     { 
      Random rand = new Random(); 

      motion = new Vector2(rand.Next(2, 6), -rand.Next(2, 6)); 
      motion.Normalize(); 

      position = new Vector2(200, 300); 

     } 


     public void Draw() 
     { 
      spriteBatch.Draw(texture, position, Color.White); 

     } 
    } 
} 
+2

좀 더 자세한 정보를 제공해주세요. 그릴 때 무슨 일이 일어날까요? 어떤 오류나 예외가 있습니까? 다른 프로세스 위에 컴포넌트를 그려서 프로세스에 숨기지 않으시겠습니까? –

+0

개체를 가져 오는 중 오류가 정의되지 않았습니다. 문제의 객체가 그냥 기본 배경 위에 그려지기 때문에 어떤 객체가 다른 객체 위에 그려지고 있다고 생각하지 않습니다. – user1719605

+0

전체 오류 메시지를 게시 할 수 있습니까? –

답변

3

"의 SpriteBatch"== null를 돌려줍니다.

 bounds = new Rectangle(0, 0, graphics.PreferredBackBufferWidth, graphics.PreferredBackBufferHeight); 

     ball = new Balloon(bounds, 1, this, spriteBatch); 
     dia = new Diamond(bounds, new Vector2(200, 200), this, spriteBatch); 

방법 "LoadContent" 이 코드를 이동해야합니다. "spriteBatch"를 만든 후.

관련 문제