This if statement works but I am confused as to how...can someone please explain how

I have an if/else if statement that doesn't work only until I change it
to an if/else statement. The problem is on line 31 where h should
increment 1 and it should because the previous if statement makes it so the
conditional will open up line 30. Here is the main issue. I need h to increment
so when I press space again a second fireball will fire off. And the weird part
is the second bullet will fire off BUT only after the first bullet crosses
the screen the FIRST time. After that, the 2nd bullet shoots??? When I tested
the program I found that h stays equal to 0 until the first bullet crosses the
screen. Can anybody see why. Thanks for any explanation. Again, this code does
work fine when I just make line 28 an else statement. I just need to understand
what is going on.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
if(keystates[SDLK_SPACE])///If space is pressed
{
    if((i==0&&h==0) || (i==0 && h==101))
        i++;///increments i to 1 to record static jump with precise y-axis value

    if(i==1 && h!=102)
    {
        staticJump=jump;
        i=2;
        h=100;
    }

    //having i = to 2 in this conditional might pose a problem if I make this program a sidescroller
    if(h==101 && i==2)
    {
        h=102;
        staticJump2=jump;
    }

    if(i==2)
        fireballShot[0]+=10;

    if(h==102)
        fireballShot[1]+=10;
}

/**HERE"S THE CONFUSION! When I simply put this as an else statement, the first statement in this function [h++] registers**/
else if(i==2 || h==102)///program works like a charm if i simply make this an else statement
{
    if(h==100)
        h++;

    if(i==2)
        fireballShot[0]+=10;

    if(h==102)
        fireballShot[1]+=10;

    ///if fireball reaches the end of the screen, bring it back to the ball
    if(fireballShot[0]>SCREEN_WIDTH /*&& fireballShot[1]>SCREEN_WIDTH*/)
    {
        i=0;
        fireballShot[0]=ballPositionXAxis;
    }
    //^^^combine these two when I figure out initial second bullet problem [less code sake]
    if(fireballShot[1]>SCREEN_WIDTH)
    {
        fireballShot[1]=ballPositionXAxis;
        h=0;
    }
}
What's the confusion? You use a control expression to limit when the clause is entered and it limits when the clause is entered.


Thanks for the reply. The confusion is....h should equal 102 when I press space a second time and it doesn't for some reason. I end up having to wait until fireballShot[0] [the 1st fireball] is greater than the screen width[line 40] AND THEN AND ONLY THEN h increments like it is supposed to. What happens in the program is when it starts I can shoot the first bullet, but not the second bullet no matter how many times I press space only until that 1st bullet crosses the screen. Then after that I can shoot both bullets. What I am going for is a player being able to shoot both bullets right off the bat.

The program does work if I change line 28 to just an else as I am able to let off two bullets right off the cuff just fine. I just really want to understand why.

EDIT: Got it. Thanks to anyone who tried this problem.
Last edited on
Topic archived. No new replies allowed.