i need make sprite shoot each time presses space. i'm pretty sure i've created texture , draw (inside player class), every time reading bullettexture.width
returns null. hope some1 can me.
here's complete rundown of code:
player.cs
using system; using system.collections.generic; using system.linq; using system.text; using microsoft.xna.framework; using microsoft.xna.framework.graphics; using microsoft.xna.framework.content; using microsoft.xna.framework.input; namespace spaceshooter_beta.animation.playercollection { public class player : spritemanager { #region field region public bool isanimating = false, isdirectional = false; private float loadtime = 0.05f, shootdelay, bulletdelay, timeelapsed; public float speed; public list<commonbullet> commonbulletlist; public int bulletcap = 20; keyboardstate kbs; #endregion #region property region public int fps { set { loadtime = (1f / value); } } #endregion #region constructor region public player(texture2d texture, int frame, int animations) : base(texture, frame, animations) { speed = 2f; shootdelay = 5f; this.addanimation("down", 1); this.addanimation("up", 2); this.addanimation("right", 3); this.addanimation("left", 4); this.addanimation("upright", 5); this.addanimation("upleft", 6); this.addanimation("downright", 7); this.addanimation("downleft", 8); commonbulletlist = new list<commonbullet>(); bulletdelay = 5f; } public void update(gametime gt) { timeelapsed += (float)gt.elapsedgametime.totalseconds; if (!this.isanimating) { if (timeelapsed > loadtime) { timeelapsed -= loadtime; if (frameindex == rects.length - 1) this.isanimating = false; else frameindex++; } } kbs = keyboard.getstate(); getmovement(); if (kbs.iskeydown(keys.space)) attack(); updatebullet(); } #endregion #region method region private void getmovement() { if (kbs.iskeydown(keys.w)) { this.pos.y -= speed; this.animation = "up"; if (kbs.iskeydown(keys.d)) { if (!isdirectional) { frameindex = 0; isdirectional = true; } this.pos.x += speed; this.animation = "upright"; } else if (kbs.iskeydown(keys.a)) { if (!isdirectional) { frameindex = 0; isdirectional = true; } this.pos.x -= speed; this.animation = "upleft"; } if (!this.isanimating) this.isanimating = true; else this.isanimating = false; } else if (kbs.iskeydown(keys.s)) { this.pos.y += speed; this.animation = "down"; if (kbs.iskeydown(keys.d)) { if (!isdirectional) { frameindex = 0; isdirectional = true; } this.pos.x += speed; this.animation = "downright"; } else if (kbs.iskeydown(keys.a)) { if (!isdirectional) { frameindex = 0; isdirectional = true; } this.pos.x -= speed; this.animation = "downleft"; } if (!this.isanimating) this.isanimating = true; else this.isanimating = false; } else if (kbs.iskeydown(keys.a)) { this.pos.x -= speed; this.animation = "left"; if (!this.isanimating) this.isanimating = true; else this.isanimating = false; } else if (kbs.iskeydown(keys.d)) { this.pos.x += speed; this.animation = "right"; if (!this.isanimating) this.isanimating = true; else this.isanimating = false; } else { this.isanimating = false; this.isdirectional = false; frameindex = 0; } if (this.pos.x <= 0) this.pos.x = 0; if (this.pos.x >= (800 - this.width)) this.pos.x = 800 - this.width; if (this.pos.y <= 0) this.pos.y = 0; if (this.pos.y >= (600 - this.height)) this.pos.y = 600 - this.height; } private void attack() { if (bulletdelay > 0) bulletdelay--; if (bulletdelay <= 0) { commonbullet cb = new commonbullet(); if(cb.bullettexture.width > 0) { console.writeline("this"); return; } cb.pos = new vector2(this.pos.x - cb.bullettexture.width / 2, this.pos.y + 15); cb.hasfired = true; if (commonbulletlist.count < bulletcap) commonbulletlist.add(cb); bulletdelay = 0; } if (bulletdelay == 0) bulletdelay = shootdelay; } private void updatebullet() { foreach (commonbullet cb in commonbulletlist) { cb.box = new rectangle((int)cb.pos.x, (int)cb.pos.y, cb.bullettexture.width, cb.bullettexture.height); cb.pos.y -= cb.bulletspeed; if (cb.pos.y < 0) cb.hasfired = false; } (int = 0; < commonbulletlist.count; i++) { if (!commonbulletlist[i].hasfired) { commonbulletlist.removeat(i); i--; } } } public void draw(spritebatch sb) { sb.draw(texture, pos, animations[animation][frameindex], color.white, rot, ori, s, se, 0f); foreach (commonbullet cb in commonbulletlist) { cb.draw(sb); } } #endregion } }
inherits spritemanager(for animation), here spritemanager class:
using system; using system.collections.generic; using system.linq; using system.text; using microsoft.xna.framework; using microsoft.xna.framework.graphics; using microsoft.xna.framework.content; namespace spaceshooter_beta { public class spritemanager { #region field region protected texture2d texture; protected rectangle[] rects; protected int frameindex = 0, frames; protected dictionary<string, rectangle[]> animations = new dictionary<string, rectangle[]>(); public vector2 pos, ori; public float rot = 0f, s= 1f; public spriteeffects se; public string animation; public int width, height; #endregion #region property region #endregion #region constructor region public spritemanager(texture2d texture, int frame, int animation) { this.texture = texture; this.frames = frame; rects = new rectangle[frame]; (int = 0; < frame; i++) { rects[i] = new rectangle(i * width, 0, width, texture.height); } width = texture.width / frame; height = texture.height / animation; } #endregion #region method region public void addanimation(string name, int row) { rectangle[] rects = new rectangle[frames]; (int = 0; < frames; i++) { rects[i] = new rectangle(i * width, (row - 1) * height, width, height); } animations.add(name, rects); } #endregion } }
i "draw" bullet texture every time called in player.cs in attack() function. here bullet class:
using system; using system.collections.generic; using system.linq; using system.text; using microsoft.xna.framework; using microsoft.xna.framework.graphics; using microsoft.xna.framework.content; namespace spaceshooter_beta.animation.playercollection { public class commonbullet { #region field region public float bulletspeed = 5f; public double bulletdamage; public texture2d bullettexture; public vector2 pos, ori; public bool hasfired; public rectangle box; #endregion #region property region #endregion #region constructor region public commonbullet() { hasfired = false; } #endregion #region method region public void draw(spritebatch sb) { sb.draw(bullettexture, pos, color.white); } public void loadcontent(contentmanager content) { bullettexture = content.load<texture2d>(@"playerbullet"); } #endregion } }
the error throws everytime @ player.cs, @ cb.pos = new vector2(this.pos.x - cb.bullettexture.width / 2, this.pos.y + 15);
. debugger, i'm pretty sure cb.bullettexture not null, it's width returns null.
i hope can me here, thanks.
you initializing new commonbullet
in every attack()
however, not calling loadcontent()
method load image.
this fixed adding loadcontent
call
commonbullet cb = new commonbullet(); cb.loadcontent(your content manager)
however, not idea load new image time, if game image, there no point.
you should make static
variable in commonbullet
static texture2d bullettexture;
you should make loadcontent
method static
, , instead of way above (calling each attack) should go main game
file, , find it's loadcontent method, , add line:
commonbullet.loadcontent(this.contentmanager);
or commonbullet.bullettexture = content.load<texture2d>("blah");
Comments
Post a Comment