Reputation: 66
I am making a game in android in which enemies are randomly spawned at the top of the screen and move down. I am able to create 1 enemy that does this, but I can't think of a good way to create many enemies that are all drawn on the same canvas. I have tried many things, and I could really use some help.
Thanks!
Upvotes: 0
Views: 583
Reputation: 101
The easiest way to do this is to create a class Enemy
(name it whatever you like) and instantiate as many as you need using a for
loop. You could use an array
to store each instance.
An example could be the following:
Enemy[] arrayOfEnemies = new Enemy[sizeOfArray];
for(int i = 0; i < arrayOfEnemies.length; i++) {
arrayOfEnemies[i] = new Enemy();
}
Then you can use an enhanced for (or for each) loop to display them wherever you'd like on your canvas.
Upvotes: 2