Results 1 to 8 of 8
  1. #1
    Join Date
    May 2005
    Posts
    102

    else if statement problem

    Hi.

    I'm trying to get this code to decrement health in a random selection and when it( health) reaches a certain number, it sleeps. Seems to ignore the Sleep() option.
    Any help would be appreciated.


    here's the main part of the code..........


    int health = 5;
    int random = rand() % 3+ 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;

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

    if(random==1)
    {
    SDL_BlitSurface(image,&rects[frame],screen,&rect);
    }
    else if(random==2)
    {
    SDL_BlitSurface(image2,&rects[frame],screen,&rect);
    health--;
    }

    else if( random==2 && health<=2) // <---SEEMS TO IGNORE THIS PART
    Sleep(2000);
    }
    else if(random==3)
    {
    SDL_BlitSurface(image3,&rects[frame],screen,&rect);
    }





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



    SDL_Flip(screen);
    frame++;
    if(frame==21)
    {
    frame=0;
    random=rand()%3+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);
    }

    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_CloseAudio();
    SDL_FreeSurface(image);
    SDL_Quit();
    return 0;


    } // end main()

  2. #2
    Join Date
    Mar 2009
    Location
    Bellevue, WA
    Posts
    803
    Try wrapping your code in code tags to make it easier to read. For example use "["code"]" at the top of your code and "["/code"]" at the end of all your code. Leave out the quotes around the brackets though, I just did that so the tags would show up in this post.
    " Imagination is the preview of life's coming attractions " - Albert Einstein
    " If you can't explain it simply, you don't understand it well enough." - Albert Einstein

    My Website: http://www.gamedevlounge.com/

    If any post is informative please provide positive feedback by clicking the star below.

  3. #3
    Join Date
    Oct 2006
    Location
    Melbourne
    Posts
    743
    Gday.

    When using the else if construct, the statement will only be evaluated if, and only if, none of the previous
    if / else if statements in that immediate block evaluate to true.

    because you have the block:
    if (random==2)
    {...
    }

    the block
    else if (random==2 && someOtherCondition)
    {...
    }

    will never be checked, since if (random==2) has already evaluated to true.

    If you take this code, and swap the two if else blocks, you'll see behaviour similar to that which your code is exhibiting.


    Code:
    #include <iostream>
    
    using std::cout;
    
    
    int main()
    {
        int a = 2, b = 1;
    
        if (a==1)
            cout << "a=1\n";
    
        else if (a==2 & b==1)
            cout << "a=2, b=1\n";
    
        else if (a==2)
            cout << "a=2\n";
    
    	return 0;
    }

  4. #4
    Join Date
    May 2005
    Posts
    102
    ....thanks. So would I just use an "else" statement instead of
    ["code"]
    else if (random==2 && someOtherCondition)
    {...
    }

    ["/code"]

    ?

  5. #5
    Join Date
    Oct 2006
    Location
    Melbourne
    Posts
    743
    That's okay.

    Though that's still not quite it. I would rephrase:


    Code:
    if(random==1)
    {
        SDL_BlitSurface(image,&rects[frame],screen,&rect);
    }
    else if(random==2)
    {
        SDL_BlitSurface(image2,&rects[frame],screen,&rect);
        health--;
    }
    
    else if( random==2 && health<=2) // <---SEEMS TO IGNORE THIS PART
        Sleep(2000);
    }
    else if(random==3)
    {
        SDL_BlitSurface(image3,&rects[frame],screen,&rect);
    }
    As either one of these two: (with an overwhelming preference for the first)

    Code:
    switch (random)
    {
        case 1:
            SDL_BlitSurface(image, &rects[frame], screen, &rect);
            break;
    
        case 2:
            if (health <= 2)
                Sleep(2000);
            else
            {
                SDL_BlitSurface(image2,&rects[frame],screen,&rect);
                health--;
            }
            break;
    
        case 3:
            SDL_BlitSurface(image3,&rects[frame],screen,&rect);
            break;
    }
    Code:
    if (random == 1)
        SDL_BlitSurfae(image, &rets[frame], screen, &rect);
    else if (random==2 & health <= 2)
        Sleep(2000);
    else if (random==2)
    {
            SDL_BlitSurface(image2,&rects[frame],screen,&rect);
            health--;
    }
    else if (random==3)
            SDL_BlitSurface(image3,&rects[frame],screen,&rect);

  6. #6
    Join Date
    Jun 2003
    Location
    Trier, Germany
    Posts
    1,350
    I second enhzflep's suggestion for switch()/case. It's definitely the cleanest solution in this case. Just don't forget about those break statements or you will fall through to your doom

    Quote Originally Posted by enhzflep View Post
    Code:
    else if (a==2 & b==1)
    Code:
    else if (random==2 & health <= 2)
    Your '&' key shows a dangerous tendency to ignore double-strokes lately

  7. #7
    Join Date
    Oct 2006
    Location
    Melbourne
    Posts
    743

    Thumbs up

    Your '&' key shows a dangerous tendency to ignore double-strokes lately
    Touchè!

    Nah, it's just a case of picnic. - Problem In Chair Not In Computer

    I did realize I'd been a little lazy when typing out those statements, and wondered if anybody would mention them.
    I suppose it's sloppy coding, though considering a==2 evaluates to 1 and b==1 also evaluates to 1, the effect of a bitwise ANDing would be identical to that of a logical ANDing.

    Mind you, thinking about it now, I can see how a flawed example may well help introduce errors into the code of others when used in different situations.

    Thanks for the 'sharpen-up' ComicSansMS.

  8. #8
    Join Date
    May 2005
    Posts
    102
    Thanks enhzflep/ComicSansMS! Really appreciate your help!!

    cheers.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •