Note that there are some explanatory texts on larger screens.

plurals
  1. POCheck each object in a ArrayList for itself
    primarykey
    data
    text
    <p>In a game that I'm working on I have implemented some powerups. I want the powerups to spawn at the same place as the zombie that you killed died on. </p> <p>I spawn all the zombies with a <code>ArrayList</code> so therefor when I kill a zombie I have to spawn the powerup at that zombies location and not any other zombies location. I got this code in a <code>paintComponent(Graphics g)</code> method (zombies is the <code>ArrayList</code>).</p> <pre><code>for(int i = 0; i &lt; zombies.size(); i++){ Zombie z = (Zombie) zombies.get(i); if(!z.isAlive()){ ImageIcon ii = new ImageIcon("Health.png"); image = ii.getImage(); g2D.drawImage(image, (int) z.getX(), (int) z.getY(), this); System.out.println("Zombie state: " + z.isAlive()); } } </code></pre> <p>The problem is that when I kill a zombie it wont spawn the health pack at that zombies location (It wont spawn it at any other zombies location either). If I remove the <code>!</code> in the if-statement a health pack will be drawn inside all of the zombies so I know that it should spawn the health pack at the right location. </p> <p>It only print out the text "Zombie state: false" once so it have something to do with when it checks if the zombie is alive or not (probably that it only counts as <code>z.isAlive</code> is false when all zombies are dead). </p> <p><code>isAlive</code> in <code>Zombie</code> is only a boolean that equals to true as default and I have set it to equal false if you shoot a zombie with this if-statement. How would I check each zombie if it's alive (which I was pretty sure that I already had done because when one zombies health equals to 50 only that zombie dies and not all of them)?</p> <pre><code>if(z.getHealth() == 50) z.setAlive(false); </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    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.
    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