Pregunta

I'm a beginner to XNA coding and am trying to make a side scroller game similar to that of Super Mario Bros. I have got player movement and enemy movement up however I am having trouble making more than one enemy and having each enemy spawn at set locations.

How would I go about coding this?
I tried to make a list of enemies and then each new enemy would be given a different position however it just ended up moving the spawn position of the first enemy to what ever the newest enemy's position is set to.

Could somebody give me an outline of the code needed?

I just need to know how to make multiple of the same enemy spawn at separate set positions along the level.

¿Fue útil?

Solución

You will need an enemy class simlar to your player class remember that since you are making more than 1 instance it cannot be static.

You will need a list of this class to hold your enemies.

List<Enemy> Enemies = new List<Enemy>();

Now you can add enemies with a random position (make some paramaters for the position in the enemy constuctor)

Random r = new Random();
...
Enemies.Add(new Enemy(new Vector2(r.Next(0,100),r.Next(0,100));

And when you need to update your enemies you can add a method in the enemy class and have this call it

foreach (Enemy enemy in Enemies)
    enemy.Update(...);
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top