Note that there are some explanatory texts on larger screens.

plurals
  1. POArray throwing exception
    primarykey
    data
    text
    <p>I can't seem to put my finger on this and why the array is not being initialized.</p> <p>Basically I am coding a 2d top down spaceship game and the ship is going to be fully customizable. The ship has several allocated slots for certain "Modules" (ie weapons, electronic systems) and these are stored in an array as follows:</p> <pre><code>protected Array&lt;Weapon&gt; weaponMount; </code></pre> <p>Upon creating the ship none of the module arrays are initialized, since some ships might have 1 weapon slot, while others have 4.</p> <p>So when I code new ships, like this example:</p> <pre><code>public RookieShip(World world, Vector2 position) { this.width = 35; this.height = 15; // Setup ships model bodyDef.type = BodyType.DynamicBody; bodyDef.position.set(position); body = world.createBody(bodyDef); chassis.setAsBox(width / GameScreen.WORLD_TO_BOX_WIDTH, height / GameScreen.WORLD_TO_BOX_HEIGHT); fixtureDef.shape = chassis; fixtureDef.friction = 0.225f; fixtureDef.density = 0.85f; fixture = body.createFixture(fixtureDef); sprite = new Sprite(new Texture(Gdx.files.internal("img/TestShip.png"))); body.setUserData(sprite); chassis.dispose(); // Ship module properties setShipName("Rookie Ship"); setCpu(50); setPower(25); setFuel(500); setWeaponMounts(2, world); setDefenseSlots(1); addModule(new BasicEngine(), this); addModule(new BasicBlaster(), this); // Add hp setHullHP(50); setArmorHP(125); setShieldHP(125); } @Override public void addModule(Module module, Ship currentShip) { // TODO Auto-generated method stub super.addModule(module, currentShip); } @Override public void setWeaponMounts(int weaponMounts, World world) { weaponMount = new Array&lt;Weapon&gt;(weaponMounts); // super.setWeaponMounts(weaponMounts, world); } @Override public String displayInfo() { String info = "Everyones first ship, sturdy, reliable and only a little bit shit"; return info; } </code></pre> <p>When I set the number of weapon mounts the following method is called:</p> <pre><code> public void setWeaponMounts(int weaponMounts, World world) { weaponMount = new Array&lt;Weapon&gt;(weaponMounts); } </code></pre> <p>This basically initializes the array with a size (weapon mounts available) to whatever the argument is. Now to me this seems fine but I have setup a hotkey to output the size of the Array, which reports zero. If I try to reference any objects in the array, it throws an outofbounds exception.</p> <p>The addModule method adds to the array as follows:</p> <pre><code> public void addModule(Module module, Ship currentShip) { currentShip.cpu -= module.getCpuUsage(); currentShip.power -= module.getPowerUsage(); if(module instanceof Engine){ engine = (Engine) module; }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>My coding ain't great but heh, better than what I was 2 month ago....</p> <p>Any ideas?</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