Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>First of all, You should avoid <code>instanceof</code>. It's not a really big deal performance-wise, but it always points to problems with your general architecture. Implement two different <code>addModule</code> methods. One that takes a Weapon, and one that takes an Engine.</p> <p>Now back to topic:</p> <pre><code>else if(module instanceof Weapon){ if (maxWeaponMounts == weaponMount.size) { System.out.println("No more room for weapons!"); } else{ maxWeaponMounts += 1; weaponMount.add((Weapon)module); } } </code></pre> <p>It looks like you use maxWeaponMounts as a counter instead of a limit. That's why I assume that it will initially be 0. The same holds for <code>Array.size</code>. It is not the limit, but <code>size</code> also counts how many elements the <code>Array</code> currently holds. Thus you will always have <code>(maxWeaponMounts == weaponMount.size)</code> as <code>0 == 0</code> and you will not add the weapon to the array. It will always stay empty and trying to reference any index will end in an <code>OutOfBoundsException</code>.</p> <p>What you should actually do is using <code>maxWeaponMounts</code> as a fixed limit and not the counter.</p> <pre><code>public void setWeaponMounts(int weaponMounts, World world) { weaponMount = new Array&lt;Weapon&gt;(weaponMounts); maxWeaponMounts = weaponMounts; } else if(module instanceof Weapon){ if (weaponMount.size &gt;= maxWeaponMounts) { System.out.println("No more room for weapons!"); } else{ weaponMount.add((Weapon)module); } } </code></pre>
 

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