I have some experience in programming but just starting to program games. I really enjoyed walking through the 2D Beginners Guide and officially creating my first game...what was driving me crazy was that the aliens were disappearing as their nose touched the left side of the screen. The drove me nuts and I knew this would be the first thing I would have to customize. I thought I would share this simple code change.
FYI - I saw another post about making the enemies going both ways...I haven't looked at it yet, but this code would have to be changed to accomplish this...I don't think it would be to hard...maybe creating an attribute to the class saying which direction the enemy is going and then put a if...else statement to handle either direction.
This is in the Game1.cs class file, in the UpdateEnemies() section:
Originally (from the tutorial) this is what is in the code:
| if (!viewportRect.Contains(new Point( |
| (int)enemy.position.X, |
| (int)enemy.position.Y))) |
| { |
| enemy.alive = false; |
| } |
My modifications:
| if (enemy.position.X < (viewportRect.Width - enemy.sprite.Width)) |
| { |
| if (!viewportRect.Contains(new Point( |
| ((int)enemy.position.X + (int)enemy.sprite.Width), |
| (int)enemy.position.Y))) |
| { |
| enemy.alive = false; |
| } |
| } |
The reason for adding the new if loop was to make sure that the enemy appeared in the window before evaluating it for reaching the other side of the screen. The only other add was adding the width of the sprite to the x position (to use the position of the back of the enemy).
Would love feedback if others find a better way to do this.