c# - Cant access list in other class with for loop -
i have real brain teaser right here , can't figure out doing wrong since code looks absolutely normal.
little background information: since i've started working in xna/monogame environment 2 days wanted start off easy. i'm building astroid defender game, , i'm working on collision check. before got collision, found got rather odd null reference while im using loop loop through enemies inside list. list in class.
upon startup initizalize spawnmanager class creates 20 enemies when class has been made so:
class spawnmanager { //draw contentmanager mycontentmanager; //enemies public list<enemy> enemy = new list<enemy>(); public void loadcontent(contentmanager thecontentmanager) { mycontentmanager = thecontentmanager; (int = 0; < 20; i++) { enemy newenemy = new enemy(new vector2((800 / (20 + 1) * + 16.5f), 20), 500, 50, 25, 10, 2500, 100, 1, 3, 2, false); newenemy.loadcontent(mycontentmanager); enemy.add(newenemy); } }
then whenever fire bullet spaceship, create bulletclass so:
public class bullet : playerscript { //enemy enemy; spawnmanager spawnmanager; playerscript playerscript; private texture2d _bullet; private rectangle _hitbox; public float xpos = 0; public float ypos = 0; public bool isactive = false; public void loadcontent(contentmanager thecontentmanager) { _bullet = thecontentmanager.load<texture2d>("graphics/citem"); _hitbox = new rectangle(0, 0, _bullet.width, _bullet.height); } public void update(gametime gametime) { //movement. ypos -= 13.5f; //collision. (int = 0; < spawnmanager.enemy.count; i++) { //console.writeline(spawnmanager.enemy.count); } }
when while im holding space fire bullets, null reference error, hightlighting loop part. i've read posts on internet saying enemy list might null or empty, if case, shouldn't matter code structure goes follows:
game1 initialize -> add spawnmanager -> add enemies + push them in enemy list.
so when enemies on screen, added enemy list , list shouldn't null or empty.
also, might there easier or more efficient way check collision? because everytime i'm creating bullet, bullet.update script start looping through enemies madman until reaches enemy, checking enemy is, applying changes , adding pool. (ofcourse when in pool wont update bullet script). since astroid defender more little bullet hell, shooting 100 bullets per few seconds, cause lot of looping in total. came on xna / monogame c# after spending 1.5 years in unity, , in unity seems simple, oncollisionenter ftw! :)
still, i'm pretty sure total id-10-t error on side, cant wrap head around it, gratefully appreciated :)
p.s: if stated unclear, i'd glad edit question!
Comments
Post a Comment