Hey im programming a simple game using directX and C++ and i have hit a slight problem which im sure some genius can fix.
I currently have an array of 8 Aliens and i currently use this code to move them left to right and down a line.
bool turnaround=false;
if ((Aliens[0].pos.x<-5 & Aliens[0].vel.x<0)|(Aliens[3].pos.x>5&Aliens[3].vel.x>0))
{
turnaround=true;
if(Aliens[0].vel.x<0)
Aliens[0].animCtrl->SetTrackSpeed(0,1);
else
Aliens[0].animCtrl->SetTrackSpeed(0,-1);
}
However this currently stops them short if ones to the left or right have been destroyed (they are destroyed by a variable Aliens[i].dead = true
I was wondering if anyone can come up with the if statement or nested if’s required to say if ones furthest left or right are destroyed go further across :/
Thanks 🙂
I presume from your question that you are constructing some sort of space invaders type game. Here would be one way to approach this that might be better than what you currently have (in a pseudo-code form):
This should cause the aliens to move down towards the bottom of the screen row by row. (Of course there are many different formations aliens could take in this type of game, this is just one). You aren’t clear about what sort of behaviour you want them to exhibit when they are killed (“go further across” is ambiguious, is this the player or some of the aliens, or what?), however, if you elaborate on that I may be able to lend further advice here.
I would also suggest you reconsider your design for a minute. You have what appears to be a fixed size array that holds the 8 aliens. While this may allow you to construct a particular level, it is likely to prove very restrictive. What if in another level you want 12 aliens? I suggest you turn your
Aliensarray into anstd::vector, so you can add them to a level as you like at will. Even better, if you have different types of enemies that all share the same properties, you could create a superclassCEnemythat these enemies all inherit from. This is likely to make your design more robust and expandable.Also, as has been stated by others already, use logical operators when you want to run an
ANDorORoperation, not bitwise. (That is, use||instead of|, etc) There is an important difference between the two.