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

[Question]

Last post 10/29/2009 12:48 PM by Greg Quinn. 6 replies.
  • 10/25/2009 3:50 PM

    [Question]

    Hello, I just recently started with XNA.  I have exeriance with vb.net and some C# and C++.  Now, I am trying to create a tower defense game were the enemies will come from the right side of the screen then when they get close to the tower, they will attack or shoot at the tower.  Now my question is, how would I make it so that when they get close enough, they start shooting?  Also, I decided to use text as the health for the tower, how would I make it so that when the health is 0 it would display a game over screen?

    Thanks in advance,

    Bapes
  • 10/25/2009 4:28 PM In reply to

    Re: [Question]

    Answer
    Reply Quote
    Hi Bapes,

    you should check out the Game State Management tutorial for help with the Game Over section.

    For your attack mechanism, you can determine the current position of the character, then subtract it from the position of the tower. When that value meets a threshold determined by yourself, you would trigger the attack. If you haven't already, check out more on Finite State Machines as this is what you would use to control the game state as well as the state of the characters.

    Hope this helps
  • 10/25/2009 4:33 PM In reply to

    Re: [Question]

    Works, thank you =D
  • 10/25/2009 4:43 PM In reply to

    Re: [Question]

    Answer
    Reply Quote
    Assuming you have an enemy class and a list or array of enemies in your game class, put a bool in the enemy class called 'isInRange'.

       //somewhere in your enemy class
      public bool isInRange(vector2 towerPosition, float maxWeaponRange)
       {
          bool result = false;
          if(Vector2.Distance(enemyPosition, towerPosition) < maxWeaponRange) result = true;
          return result;
       }

    then.... in the game or tower class' update method:

    foreach(enemy en in enemies)
    {
       if(en.isInRange(towerPosition, weaponRange))
          {
             //toggle shooting on or branch to shoot sequence
          }
    }

    for the health question, store a float or integer in the game class and in each update method, check it for value, if it is < 0, run the game over sequence. Every time an enemy scores a hit against the tower, decrement the stored float or integer slightly.
    Unlock The *I don't use 3 angles in a Vector3 for rotations anymore* Achievement Here
  • 10/25/2009 4:48 PM In reply to

    Re: [Question]

    Thanks, I'm going to try that out.  I just need to have my friend draw the sprites first.  Also, does anyone know of a in depth video guide to animating 2d sprites?
  • 10/26/2009 3:50 AM In reply to

    Re: [Question]

    You should find the "How To: Animate A Sprite" tutorial under the "XNA Game Studio Documentation" that is installed as part of XNAGS. It isn't a video guide, but it should do.
  • 10/29/2009 12:48 PM In reply to

    Re: [Question]

    Good luck with your tower defense game.
    Just played this online a few days ago, a flash based version quite addictive.

    Try get some good little sound effects in there too.
Page 1 of 1 (7 items) Previous Next