c# - Error creating a hitbox? -
so have healthpickup class , player class. each class has line of code:
public static rectangle hitbox = new rectangle(pos.x, pos.y, tex.width, tex.height); my question is, why there error on player class , not healthpickup class? edit: if step on player hitbox, healthpickup causes same error.is rectangle? error typeinitializationexception details follow such:
system.typeinitializationexception unhandled hresult=-2146233036 message=the type initializer 'testgame.player' threw exception. source=testgame typename=testgame.player stacktrace: @ testgame.rpg.loadcontent() in c:\users\pyroglyph\documents\visual studio 2010\projects\testgame\testgame\testgame\rpg.cs:line 41 @ microsoft.xna.framework.game.initialize() @ testgame.rpg.initialize() in c:\users\pyroglyph\documents\visual studio 2010\projects\testgame\testgame\testgame\rpg.cs:line 33 @ microsoft.xna.framework.game.rungame(boolean useblockingrun) @ microsoft.xna.framework.game.run() @ 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 instance of object. source=testgame stacktrace: @ testgame.player..cctor() in c:\users\pyroglyph\documents\visual studio 2010\projects\testgame\testgame\testgame\player.cs:line 20 innerexception: edit: stopped error still need collision-detection, i'm using console.beep() determine whether code runs or not. and, 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 way can check if it's beingness 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) { } } }
this exception thrown whenever static constructor throws exception, , in case seems have nullreferenceexception in static constructor of player class.
edit:
the problem here tex static field isn't initialized before hitbox created, , explains reason of nullreferenceexcpetion get. in order solve problem have initialize it, , can in way in player class:
public static rectangle hitbox; //other fields , 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); } as can see created method cause problem comes when graphics resources needed initialize. in xna initialize method called before loadcontent, need overcome problem create method , phone call after loading content. game class can done as:
public class mygame : microsoft.xna.framework.game { public static contentmanager contentmanager; //... public mygame( ) { //... contentmanager = content; } protected override void initialize(){ // todo: add together initialization logic here base.initialize(); } protected override void loadcontent() { spritebatch = new spritebatch(graphicsdevice); // todo: utilize this.content load game content here //... this.initializeafterloadingcontent(); } //you can phone call want private void initializeafterloadingcontent(){ //initialize non-graphical resources using content info need, //like width , height of texture loaded } //... } c# xna xna-4.0
No comments:
Post a Comment