XNA Creators Club Online
Page 1 of 1 (3 items)
Sort Posts: Previous Next

2D Beginners Guide - Early Kill

Last post 11/17/2009 3:05 AM by Hattsoffs. 2 replies.
  • 11/16/2009 3:29 AM

    2D Beginners Guide - Early Kill

    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.
  • 11/16/2009 4:55 AM In reply to

    Re: 2D Beginners Guide - Early Kill

    You might want to check if the enemy is alive first before doing any other comparisons.  I don't really remember but won't only enemies that are alive appear on the screen?  If so you could replace your first check altogether.
    University of New Mexico Advanced Graphics Lab -- 3 Games for Fall 2009: Marauder Madness! in review | Magnetic Defender on XBLIG | Toybox Racing on XBLIG
  • 11/17/2009 3:05 AM In reply to

    Re: 2D Beginners Guide - Early Kill

    Yeah...you are right, you want to check that the enemy is alive first and that is in the code (I just didn't want to copy the entire piece in).  When the enemy is initialized the back of the enemy is off the viewportRect.  So as the enemy is created and then updated, the section of code is executed and (without my changes) the point isn't within the viewportRect and the enemy is killed.  So you have to delay killing the enemy until after the entire enemy is displayed in the viewport rect (or shot by a cannonball).


Page 1 of 1 (3 items) Previous Next