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 ...
Get Learning Java by Building Android Games - Second Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.