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 deleted 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();
We have an array called mBullets
, capable of holding 10000 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.
We also declare and initialize two objects of type Random
which we will use to randomly generate screen positions...