2014-06-22 4 views
-1

그래서 HealthPickup 클래스와 Player 클래스가 있습니다. 각 클래스에는 다음과 같은 코드 행이 있습니다.히트 박스를 만드는 중 오류가 발생 했습니까?

public static Rectangle Hitbox = new Rectangle(Pos.X, Pos.Y, Tex.Width, Tex.Height); 

내 질문은 왜 플레이어 클래스에 오류가 있고 HealthPickup 클래스에 오류가 있습니까? 편집 : 나는 플레이어 hitbox 이상의 단계 HealthPickup 같은 오류가 발생합니다. 그것은 내 사각형 함께 할 수 있습니까?

System.TypeInitializationException was unhandled 
    HResult=-2146233036 
    Message=The type initializer for 'TestGame.Player' threw an exception. 
    Source=TestGame 
    TypeName=TestGame.Player 
    StackTrace: 
     at TestGame.RPG.LoadContent() in C:\Users\Pyroglyph\Documents\Visual Studio 2010\Projects\TestGame\TestGame\TestGame\RPG.cs:line 41 
     at Microsoft.Xna.Framework.Game.Initialize() 
     at TestGame.RPG.Initialize() in C:\Users\Pyroglyph\Documents\Visual Studio 2010\Projects\TestGame\TestGame\TestGame\RPG.cs:line 33 
     at Microsoft.Xna.Framework.Game.RunGame(Boolean useBlockingRun) 
     at Microsoft.Xna.Framework.Game.Run() 
     at TestGame.Program.Main(String[] args) in C:\Users\Pyroglyph\Documents\Visual Studio 2010\Projects\TestGame\TestGame\TestGame\Program.cs:line 15 
    InnerException: System.NullReferenceException 
     HResult=-2147467261 
     Message=Object reference not set to an instance of an object. 
     Source=TestGame 
     StackTrace: 
      at TestGame.Player..cctor() in C:\Users\Pyroglyph\Documents\Visual Studio 2010\Projects\TestGame\TestGame\TestGame\Player.cs:line 20 
     InnerException: 

편집 : 나는 오류를 중지하지만, 난 여전히 코드를 실행 여부 결정() 나는 Console.Beep을 사용하고, 충돌 감지를 필요로하는 오류는 다음과 같은 수행을 자세히 설명한 TypeInitializationException이다. 그리고, 더 많은 코드 : 무승부

public class HealthPickup : Microsoft.Xna.Framework.GameComponent 
{ 
    public static Texture2D Tex; 
    public static Point Pos = new Point(50, 50); 
    public static Rectangle Hitbox = new Rectangle(Pos.X, Pos.Y, 32, 32); 

    public HealthPickup(Game game) : base(game) { } 

    public override void Initialize() { base.Initialize(); } 

    public override void Update(GameTime gameTime) 
    { 
     //if (Tex.Bounds.Intersects(Player.Tex.Bounds)) //was testing other collision-detection methods 
     //{ 
     // Console.Beep(); //the only way I can check if it's being run or not 
     //} 
    } 
} 

() 요청에 따라 :

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 TestGame 
{ 
    public class Player : Microsoft.Xna.Framework.GameComponent 
    { 
     public static Texture2D Tex; 
     public static string Dir = "D"; 
     public static Point Pos = new Point(GraphicsDeviceManager.DefaultBackBufferWidth/2, GraphicsDeviceManager.DefaultBackBufferHeight/2); 
     public static int speed = 4; 
     public static Rectangle Hitbox = new Rectangle(Pos.X, Pos.Y, Tex.Width, Tex.Height); 

     public Player(Game game): base(game) { } 

     public override void Initialize() { base.Initialize(); } 

     public override void Update(GameTime gameTime) { } 
    } 
} 
+0

플레이어 클래스 코드를 게시 할 수 있습니까? 그게 문제가 아니라 다른 것을 포함하고 있기 때문입니다. 감사. –

+0

@Fuex 저는 현재 PC에 없지만, Player 클래스 (모바일 앱 사용)로 내일 다시 보겠습니다. – Pyroglyph

+0

@Fuex 좋아요. 'Player.cs' 코드가 게시되었습니다! 내게 비판 할 수있는 것을보고 보라. :) – Pyroglyph

답변

1

이 예외가 발생 정적 생성자에서 예외가 발생 할 때마다 :

GraphicsDevice.Clear(Color.White); 

     spriteBatch.Begin(); 

     // Create Useless Rectangles 
     Rectangle player = new Rectangle(Player.Pos.X, Player.Pos.Y, Player.Tex.Width, Player.Tex.Height); 
     Rectangle healthPickup = new Rectangle(HealthPickup.Pos.X, HealthPickup.Pos.Y, HealthPickup.Tex.Width, HealthPickup.Tex.Height); 

     // Draw Sprites 
     spriteBatch.Draw(Player.Tex, player, Color.White); 
     spriteBatch.Draw(HealthPickup.Tex, healthPickup, Color.White); 

     spriteBatch.End(); 

     base.Draw(gameTime); 

플레이어 클래스 이 경우에는 클래스의 정적 생성자에 NullReferenceException이있는 것으로 보입니다.

편집 :

여기서 문제가 HitBox 전에 초기화되지 않은 Tex 정적 필드가 작성하고, 이것이 당신이 얻을 NullReferenceExcpetion의 이유를 설명합니다. 그래서 당신이 그것을 초기화해야하고, 당신은 Player 클래스에이 방법으로 그것을 할 수있는 문제를 해결하기 위해 : 그래픽 자원을 때 당신은 내가 다른 방법이 문제가 발생할 만들어 볼 수 있듯이

public static Rectangle HitBox; 
//other fields and methods 

public override void LoadContent(){ 
    ContentManager content = MyGame.ContentManager; 
    Tex = content.Load<Texture2D>("mytexture"); 
    //... 

    base.LoadContent();  
    this.InitializeAfterLoadingContent(); 
} 

private void InitializeAfterLoadingContent(){ 
    Hitbox = new Rectangle(Pos.X, Pos.Y, Tex.Width, Tex.Height); 
} 

제공 초기화해야했습니다. XNA에서 Initialize 메서드는 LoadContent 전에 호출되므로이 문제를 극복하기 위해 필요한 것은 메서드를 만들고 내용을로드 한 후에 호출하는 것입니다. 또한 게임 클래스는 다음과 같이 수행 할 수 있습니다 :

public class MyGame : Microsoft.Xna.Framework.Game { 
    public static ContentManager ContentManager; 
    //... 

    public MyGame() { 
     //... 

     ContentManager = Content; 
    } 

    protected override void Initialize(){ 
     // TODO: Add your initialization logic here 

     base.Initialize(); 
    } 

    protected override void LoadContent() { 
     spriteBatch = new SpriteBatch(GraphicsDevice); 

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

     this.InitializeAfterLoadingContent();    
    } 

    //you can call it however you want 
    private void InitializeAfterLoadingContent(){ 
     //initialize your non-graphical resources by using the content info you need, 
     //like the width and height of a texture you loaded 
    } 

    //... 
} 
+0

그래서 어떻게이 NullReferenceException을 고칠 수 있습니까? 분명히 내 사각형/Hitbox null입니다, 어떻게 해결할 것이라고? – Pyroglyph

+0

더 많은 코드가 필요합니다. 게시 할 수 있다면 기꺼이 도와 드리겠습니다. –

+1

@Pyroglyph 당신은 항상 ctor를 단계별로 실행하거나 디버거를 통해 로그 할 수 있습니다. – bubbinator

관련 문제