So I have a HealthPickup class and a Player class. Each class has this line of code:
public static Rectangle Hitbox = new Rectangle(Pos.X, Pos.Y, Tex.Width, Tex.Height);
My question is, why is there an error on the Player class and not the HealthPickup class? Edit: If i step over the Player hitbox, the HealthPickup causes the same error.Is it something to do with my Rectangle? The error is a TypeInitializationException which details follow as such:
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:
Edit: I stopped the error but I still need collision-detection, I'm using Console.Beep() to determine whether code runs or not. And, as per request, more code :)
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
//}
}
}
In Draw():
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);
Player class:
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) { }
}
}