question on decrementing

Hi,

I'm trying to decrement health so when it gets to a certain level it will call up another animation, but it doesn't seem to work right. Any help would greatly be appreciated.









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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#include <SDL/SDL.h>
#include <SDL/SDL_mixer.h>
#include "rects.h"
#include <cstdlib>



int main( int argc, char* args[] )
{   
    SDL_Init(SDL_INIT_EVERYTHING);
    SDL_Surface* screen,*image, *image2,*image3, *image4, *image5;
    Mix_Music* music , *music2, *music3 ,*music4, *music5;
    Mix_OpenAudio(22050,MIX_DEFAULT_FORMAT,2,4096);
    music=Mix_LoadMUS("fly.wav");
    music2=Mix_LoadMUS("run.wav");
    music3=Mix_LoadMUS("jump.wav");
    music4=Mix_LoadMUS("duck.wav");
    music5=Mix_LoadMUS("strafe.wav");
    screen=SDL_SetVideoMode(640,480,32,SDL_SWSURFACE);
    bool running=true;
    const int FPS=20;
    Uint32 start;
    SDL_Rect rect;
    int frame=0;
    rect.x=100;
    rect.y=100;
    rect.w=50;
    rect.h=50;
    Uint32 color=SDL_MapRGB(screen->format,0x00,0x00,0x00);
    Uint32 color2=SDL_MapRGB(screen->format,0xff,0xff,0xff);
    image=SDL_DisplayFormat(SDL_LoadBMP("fly.bmp"));
    image2=SDL_DisplayFormat(SDL_LoadBMP("run.bmp"));
    image3=SDL_DisplayFormat(SDL_LoadBMP("jump.bmp"));
    image4=SDL_DisplayFormat(SDL_LoadBMP("duck.bmp"));
    image5=SDL_DisplayFormat(SDL_LoadBMP("strafe.bmp"));
    SDL_Rect rects[21];  
    setrects(rects);
    SDL_SetColorKey(image,SDL_SRCCOLORKEY,SDL_MapRGB(screen->format,0x00,0xff,0xff));        
    
//////////////////////////////////////////////////////////////////////////////////////////////////////
    
    
    int health = 100;
    int random = rand() % 5+ 1 ;

	while(running)
	{
          
          
		  start=SDL_GetTicks();
		  SDL_Event event;

	      while(SDL_PollEvent(&event))
		  {
              
		   switch(event.type)
		   {
		     case SDL_QUIT:
		     running=false;   
		     break;

		     }
		  }


                                  
SDL_Rect rect;
rect.x=0;    
rect.y=0;

/////////////////////////////////////////////////////////////////////////////////////////////////////


        switch (random)
{
    case 1:
        SDL_BlitSurface(image, &rects[frame], screen, &rect);
	health--;

        break;

    case 2:

        SDL_BlitSurface(image2,&rects[frame],screen,&rect);
	health--;

        break;

    case 3:

        SDL_BlitSurface(image3,&rects[frame],screen,&rect);
	health--;

        break;

    case 4:
                
        if (health <= 20)   // <------ here's the problem
            SDL_BlitSurface(image4,&rects[frame],screen,&rect);
        else
        {
        SDL_BlitSurface(image5,&rects[frame],screen,&rect);
	health--;
	health--;
        }
        break;

    case 5:
        SDL_BlitSurface(image5,&rects[frame],screen,&rect);
	health--;
	health--;

        break;


}



        
////////////////////////////////////////////////////////////////////////////////////////////////////////
 
          
 
          SDL_Flip(screen);
          frame++;

		  if(frame==21)
         {
           frame=0;
           random=rand()%5+1;
           Mix_HaltMusic();
          if(random==1)
          Mix_PlayMusic(music,0);
          else if(random==2)
          Mix_PlayMusic(music2,0);
          else if(random==3)
          Mix_PlayMusic(music3,0);
          else if(random==4)
		  {
          if (health <= 20)
          Mix_PlayMusic(music4,0);
		  }
          else if(random==5)
          Mix_PlayMusic(music5,0);
          }


   if(1000/FPS>SDL_GetTicks()-start )
   SDL_Delay(1000/FPS-(SDL_GetTicks()-start));
          
  

}   // end while()


Mix_FreeMusic(music);
Mix_FreeMusic(music2);
Mix_FreeMusic(music3);
Mix_FreeMusic(music4);
Mix_FreeMusic(music5);
Mix_CloseAudio();
SDL_FreeSurface(image);
SDL_Quit();
return 0;

		  
}   // end main()
Damn ... I'm not too good with SDL, but to assess the problem, why not lower the health to 20, remove the five cases in the switch statement to just 1 and see if what you intended to work happens?

That way you'll know if it's a problem within another problem or not.
as pointergirl stated, try removing parts of the code to single out what exactly is going wrong. Try modifying the code to maybe find a solution. Good luck!
....ya.. Ive actually modified it a couple times. I'm thinking it might be something to do with where the "int health"declaration is placed . It should decrement in every switch case statement, but im not sure how to display the "health" value in order to see if it's actually decrementing. I could test the value in console app but not win 32. (maybe printF?)
Topic archived. No new replies allowed.