c# - Have an array of Asteroids, how can I loop them to infinitely spawn? -


i have made game similar asteroids , have made array of asteroids controlled int count have made spawn 10 asteroids screen when game starts.

what i'm wondering how can asteroids spawn infinitely. i'm thinking of using loop , have tried:

if (asteroidcount <= 5) {     asteroidcount += 10; }  

but doesn't seem work. using visual studio express c# 2010

i think need try different approach. first need asteroid class can store positions , other variables may need.

public class asteroid {        public vector2 velocity;        public vector2 position;        public asteroid(vector2 velocity, vector2 position)        {         velocity = velocity;         position = position;        } } 

now add list game, store asteroids. reason chose on array is easier change size depending on how many asteroids have.

list<asteroid> asteroids = new list<asteroid>(); 

now can spawn 10 asteroids @ begining of game

for (int = 0; i<10;i++) { asteroids.add(new asteroid(new vector2(0,10), new vector2(50,50))); } 

that make asteroid @ position 50,50 velocity of 10, if use update code below move down velocity.

now, actual problem, need spawn more when not enough (player destroyed them assume)

so, in update method:

while (asteroids.count <5) //if there less 5 asteroids, add more { asteroids.add(new asteroid(new vector2(0,10), new vector2(50,50))); //same thing before, add asteroid } 

and there go!


here extra tips

if want draw asteroids, need make method it

 public void drawasteroid(asteroid a)  {         spritebatch.draw(asteroid texture, a.position, color.white);         spritebatch.end();  } 

now in draw() method can add this

  spritebatch.begin();   foreach (asteroid in asteroids) //draw each astroid   {        drawasteroid(a);   }   spritebatch.end(); 

and can use simlar approach if want update of asteroids. in update(),

float elapsed = (float)gametime.elapsedgametime.totalseconds; foreach (asteroid in asteroids) //update each astroid {     updateasteroid(a, elapsed); } 

and method,

public void updateasteroid(asteroid a, float elapsed) {        a.position += a.velocity * elapsed; } 

Comments

Popular posts from this blog

python - How to create a legend for 3D bar in matplotlib? -

java - Multi-Label Document Classification -

php - Dynamic url re-writing using htaccess -