Note that there are some explanatory texts on larger screens.

plurals
  1. POSpace Invaders health/array issues
    primarykey
    data
    text
    <p>I've been doing some exercises to learn C#. I've been doing XNA and making a space invaders clone. </p> <p>So far, everything is dandy, but I have come across some walls when coding. </p> <p>The issues and the supporting code are as follows:</p> <p>My top of row of invaders have 2 health points, take 2 hits to destroy and yield more points. However, when one is hit, and destroyed, the rest of the top of row are reduced to 1 HP, and take 1 hit to destroy - which is not my desired result. </p> <p>The offending code I suspect is: </p> <pre><code> if (playerBullet != null &amp;&amp; Type1Invaders != null) { Rectangle rectMissile = new Rectangle((int)playerBullet.getX(), playerBullet.getY(), playerBulletIMG.Width, playerBulletIMG.Height); for (int count = 0; count &lt; 11; count++) { Rectangle rectInvader = new Rectangle(Type1Invaders[count].getX(), Type1Invaders[count].getY(), invader1.Width, invader1.Height); if (Type1Invaders[count].getVis() &amp;&amp; rectMissile.Intersects(rectInvader)) { Type1Invaders[count].setHP(Type1Invaders[count].getHP() - 1); shootTimer = 0; if (Type1Invaders[count].getHP() == 0) { explosionInstance.Play(); playerBullet = null; Type1Invaders[count].setVis(false); score = score + Type1Invaders[count].getPointValue(); } } } } </code></pre> <hr> <p>My second error resides in how I'm detecting the leftmost and rightmost invaders in a row. When an entire row has been destroyed, I get a nullreferenceerror. (Those are a nightmare..)</p> <p>Anyway, this is the offending code</p> <p>The method of finding the left and right most invaders</p> <pre><code>var LeftMost5 = Type5Invaders.Where(i =&gt; i.getVis()).FirstOrDefault(); var RightMost5 = Type5Invaders.Where(i =&gt; i.getVis()).LastOrDefault(); </code></pre> <p>And the if statement is throwing the null error</p> <pre><code> if (RightMost5.getX() &gt;= RightGameEdge) { Type5.setDir(-1); for (int count = 0; count &lt; 11; count++) { invaderMoveInstance5.Play(); Type5Invaders[count].MoveY(8); } } </code></pre> <p>It only happens with the rightmost, but I can assume it will happen to the left too - I'm assuming I can apply the same logic to fix this error to the left side too. </p> <p>I can supply more information and snippets if this is not sufficient. </p> <p>Thanks in advance for the assistance!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload