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

Iterator not working

Last post 2/9/2010 6:20 PM by James Walsh II. 8 replies.
  • 2/8/2010 5:15 PM

    Iterator not working

    I get an error saying that the hp++ is unreachable code but I don't understand why or how to overcome it.

     

    const int hitPoints = 2;  
    ....  
    for (int hp = 0; hp < hitPoints; hp++) 

    Thanks

     

  • 2/8/2010 5:17 PM In reply to

    Re: Iterator not working

    What's the code in the for loop look like?
    Jim Perry - Microsoft XNA MVP
    If people spent a minute searching the forums and reading the FAQs before posting I'd be out of a job.
      Got some XNA Game Studio/XNA Framework development info to share with the community? Put it on the XNA Wiki.
        Please mark posts as Answers or Good Feedback when appropriate.
  • 2/8/2010 5:38 PM In reply to

    Re: Iterator not working

    It is a modification of the 2d ufo tut

     

       if (cannonBallRect.Intersects(enemyRect))  
                            {  
                                ball.alive = false;  
                                  
                                for (int hp = 0; hp < hitPoints; hp++)  
                                {  
                                      
                                    enemy.alive = false;  
                                    score += 1;  
                                    break;  
                                }  
                                 
                            } 

    I maybe doing this wrong. I was trying to setup a hit point counter on the ufos.

     

  • 2/8/2010 5:42 PM In reply to

    Re: Iterator not working

    You break out of the loop after a single iteration, so it will never execute the hp++ code.
  • 2/8/2010 6:05 PM In reply to

    Re: Iterator not working

    I am missing some concept then. I was under the impression that since hp starts less then two it would iterate until it got to two then it would execute the rest of the code.
  • 2/8/2010 6:10 PM In reply to

    Re: Iterator not working

    I would also rethink what your trying to do... & work out on paper before u start coding....

    do u really want to use a for loop anyway?

    your also resetting it to 0 in the for loop... & does hp mean health points or hit points?   u would be checking the enemys health...

    So if u removed that break; u will see it still does not work... as the score will go up till it gets to the hitpoints value + 1...

    I would get a ebook on c# programming if u can't trace it... or put a breakpoint on it after u remove the break;.  

    It would only be dead if health is less than or equal to 0.


  • 2/8/2010 6:22 PM In reply to

    Re: Iterator not working

    I redid this using an if statement. It works correctly except that it has one set of hitpoints for all the enemies. So now I need to figure out how to make each ship have its own hitpoints ( I can work on this and I am not looking for an answer right now).

     if (cannonBallRect.Intersects(enemyRect))  
                            {  
                                ball.alive = false;  
                                  
                                if (hp < hitPoints)  
                                {  
                                    hp++;  
                                }  
                                else 
                                {  
                                      
                                    enemy.alive = false;  
                                    score += 1;  
                                    break;  
                                }  
                                 
                            } 
  • 2/8/2010 6:22 PM In reply to

    Re: Iterator not working

    James Walsh II:
    I am missing some concept then. I was under the impression that since hp starts less then two it would iterate until it got to two then it would execute the rest of the code.

    The keyword "break" forces execution out of the loop when it hits it. You need to call "break" only when the condition exists to exit the loop before it finishes. You don't have that currently.
    Jim Perry - Microsoft XNA MVP
    If people spent a minute searching the forums and reading the FAQs before posting I'd be out of a job.
      Got some XNA Game Studio/XNA Framework development info to share with the community? Put it on the XNA Wiki.
        Please mark posts as Answers or Good Feedback when appropriate.
  • 2/9/2010 6:20 PM In reply to

    Re: Iterator not working

    Ok, I understand now. Thanks.
Page 1 of 1 (9 items) Previous Next