Spawning an array of bullets
Now we know the basics of arrays we can get started spawning a load of bullets at the same time and store them in an array.
Tip
Make sure you delete the temporary code from the Spawning a bullet section before proceeding.
Add a few control variables and declare an array of bullets as a member of BulletHellGame. Add this code just before the constructor:
// Up to 10000 bullets private Bullet[] mBullets = new Bullet[10000]; private int mNumBullets = 0; private int mSpawnRate = 1; private Random mRandomX = new Random(); private Random mRandomY = new Random();
Important note
You will need to add the Random class: import java.util.Random;.
We have an array called mBullets, capable of holding 10,000 bullets. The new keyword initializes the array, not the Bullets within the array. We also have two int variables to keep track of how many bullets we want to spawn each time the spawn method is called and how many bullets there are in total.