Problem with WM_KEYDOWN

I'm new to C++ win32 programming and I'm having some problems when I try to execute the following code:

keyPressed[0] == true when w is pressed in the keyboard otherwise it is false.


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
if(n1.getYPos() < 340)
{
    renderer->drawSprite(n1.getTexture()->getID(), 64,128, n1.getXPos(),      
                         n1.getYPos(), 24,8,19,1);
    n1.updateYPos();
    if(keyPressed[0])
    {	
      	score -= 50;
	n1.setYPos(-50);
    }
}
else if (n1.getYPos() >= 340 && n1.getYPos() <= 453)
{
    renderer->drawSprite(n1.getTexture()->getID(), 64,128, n1.getXPos(),                                     
                         n1.getYPos(), 24,8,19,1);     
    n1.updateYPos();
    if(keyPressed[0])
    {
	score+=50;
	n1.setYPos(-10);
     }
}
else 
{
    score =0;;
    n1.setYPos(0);
}


The issue here is that even if the program entered the else if clause, it still executes the if clause which I presume should not happen. (the code above is inside a game loop)..

Any help would be greatly appreciated! :))
Last edited on
Are you sure it's going into both blocks in the same loop iteration? I don't see how ;-)

It's more likely that first time around, YPos is > 340 so the else-if or else blocks sets it to -10 or 0, respectively.

Then the next time round YPos is < 340, so it goes in the 1st if block.

Cheers,
Jim
Solved my problem by introducing a time delay for every iteration of the game loop!
Topic archived. No new replies allowed.