Updating every instance of class

Started by
0 comments, last by jefferytitan 11 years, 10 months ago
I am finishing off my Space Invaders game in XNA. One stumbling block I am having is getting all the martians to change direction whenever the furthest right ones reach the end of the screen. Difficult to post code without posting large chunks, but what I have is

Ususal Game1 class
Player class (Drawable component)
Martian class with about 48 instances

What I am basically trying to do from the Game class is

If last column have reached the end of the screen
{
For Each instance of Martian
{
change Xspeed to -Xspeed
}
}

I have tried using foreach Game Component in Components but that hasn't worked. I could make a public struct Martian in the Game1 class but that seems to take away some object orientedness.
Advertisement
Make sure you're clear about the way things fail. They don't change direction? Every object on the screen changes direction? They jitter back and forth?

You could store a collection of your Martians so it's easy to loop through. Or (as their velocity is always in lockstep) you could make it a static property.

This topic is closed to new replies.

Advertisement