Search code examples
c#if-statementxnadrawstring

C# and XNA display more than one text


I want to show more than one text with the C# and XNA. The situation is like this. If A then display AAAAA; If B after 'A' then display BBBB and AAAA at the same time.
Here is the code I wrote:

if (Score == A)
{
    spriteBatch.DrawString(
        Font,                          
        "AAAAAAAAAAAAAAAAAA",  
        scorePos,                     
        Color.White);
}
 if (Score == B)
{
    spriteBatch.DrawString(
        Font,                         
        "BBBBBBBBBBB",  
        scorePos2,                     
        Color.White);
}

However, if I display BBB, AAA disappears; what I should I use instead of my If statement?


Solution

  • Its still not entirely clear what behavior you want, but it seems you don't understand what else does.

    An else (or else if) statement will only execute if the if/else if statements above it do not.

    With your code, this means that you will only ever execute the "A" block or the "B" block, never both.

    If you just want "A" to print As and "B" to print Bs (independent of each other), just don't use else:

    if (A)
    {
    }
    if (B)
    {
    }
    

    If you only want to print Bs if you have As, then nest them:

    if (A)
    {
       if (B)
       {
       }
    }
    

    Honestly, I doubt you want any else structures in your current code. You do have the additional problem of checking equality on the Score variable. Score can never equal "A" and "B" at the same time, so you could never get into both conditions as it stands anyways. You'll need to find a different condition to check.