-3

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?

  • `If A then display AAAAA, if B then display BBBB` I don't see that logic in your code, only a comparison to the Score. – gunr2171 Sep 26 '14 at 15:41
  • Can you give a concrete example? I don't see why you can't just remove the if block here. You know what conditions are, right? And your A B conditions don't really translate well with your code example that follows. – Pierre-Luc Pineault Sep 26 '14 at 15:42
  • Hello, thank you for your reply. I understand there is problem in the code. Other than using "If else", what should I use please? – user3684119 Sep 26 '14 at 15:43
  • Hello all, thank you all for your kindly reply. My situation is like this: If score == A , then show "AAAAA", or score== B, then show "BBBB" but I still want to show "AAAAA" on the screen when I show "BBBB", what should I do please? – user3684119 Sep 26 '14 at 15:48
  • Why do you have `If score == A , then show "AAAAA"` if `AAAAA` has to be displayed whatever the state is? Just display `AAAAA` outside of the `if` clause. Your requirement is not clear. – Patrice Gahide Sep 26 '14 at 15:49
  • Sorry, it is my fault to confuse everybody. Let me explain it again. I have two situation. Either A or B, and not matter which one appear first, I want to display the corresponding text, what is more, If A appear first, and B appear later, when I show "BBB" and I still want to show "AAA" on the screen. – user3684119 Sep 26 '14 at 15:57
  • You could say `"BBBBBBBBBBB" + "AAAAAAAAAAAAA"` - that makes one string out of two. – Cullub Sep 26 '14 at 15:59
  • Thanks, but if I have more than 10 situations, this method might be a little bit difficult. – user3684119 Sep 26 '14 at 16:05
  • You've just introduced a time factor in the equation. So the same method is called multiple times, and if `AAA` has already been displayed in a previous call, it needs to stay where it was. Is that it? It's still not clear how `A` should be updated, whether `B` should be cleared between two calls, and so on. Posting the whole method (with its signature) and explaining the role of each variable would help. – Patrice Gahide Sep 26 '14 at 16:12

2 Answers2

0

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.

BradleyDotNET
  • 60,462
  • 10
  • 96
  • 117
  • Thanks for reply. I have typo before and I fixed it. Yes, you are right, I can't have both A and B if I use "else" statement. – user3684119 Sep 26 '14 at 16:23
  • @user3684119 You still have the problem of mutually exclusive conditions. You need to find a different way to determine which to draw, because they will never be true together. – BradleyDotNET Sep 26 '14 at 16:27
  • I have thought about using nested if as your second example. But either A or B will appear first, so the outer if might be difficult to decide. And I have A,B,C,D, etc, the nested if would be a little bit difficult? Thanks again. – user3684119 Sep 26 '14 at 16:30
  • @user3684119 It all depends on what the rules are. Only nest if performing the inner code depends on the outer condition being true. – BradleyDotNET Sep 26 '14 at 16:32
0

thanks for all the reply. I find my question stupid when I figure out the answer. Just define two variable can solve the problem, the modified coed is like

        if (Score == 1)
        {
            spriteBatch.DrawString(
                Font,                          // SpriteFont
                "AAAAAAA",  // Text
                scorePos,                      // Position
                Color.White);
         }//end of H2

        ////If O2 matched, score=2
        if (Score2 == 1)
        {
            spriteBatch.DrawString(
                Font,                          // SpriteFont
                "BBBBBBBB",  // Text
                scorePos3,                      // Position
                Color.White);
        }//end of O2