"walking" animation

This is a discussion on "walking" animation within the C++ Programming forums, part of the General Programming Boards category; Say I have one big bitmap with all the diff. pictures for a guy walking, or i had all diff. ...

  1. #1
    Unregistered Leeman_s's Avatar
    Join Date
    Oct 2001
    Posts
    753

    "walking" animation

    Say I have one big bitmap with all the diff. pictures for a guy walking, or i had all diff. bitmaps for it. Either way, I'm wondering if this would work:

    I could declare an integer whose value is 10, and decrement it each time the user hits the key that moves the guy. then when i draw the guy to the screen, i check where the int is at and then put it in a switch statement and depending what it is it will display one of the states of the walking position. and when it gets to zero, reset it to 10. (assuming there are 10 diff walking positions). is this a way to do it or is there something more efficient?

  2. #2
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Sure thing Or, at least, that's how I would do it, if you want the unprofessional opinion
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  3. #3
    Unregistered Leeman_s's Avatar
    Join Date
    Oct 2001
    Posts
    753
    oh, also, is this a good way to check for keypresses?:
    Code:
    case WM_KEYDOWN:
    		{
    		    int KEY_LEFT = GetKeyState(VK_LEFT);
    			if(LOWORD(KEY_LEFT))
    			{
    				MessageBox(hWnd, "Pressed", "Pressed", MB_OK);
    			}
    		}

  4. #4
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Hmm, I'd have to say no. Try this instead:
    PHP Code:
    case WM_KEYDOWN:
         switch((int)
    wParam)
         {
         case 
    'W':
              
    //what happens when W is pressed
              
    break;
         case 
    VK_LEFT:
              
    //pop up the messagebox
              
    break;
         }
         return 
    0
    If it's a button (i.e. shift, enter, arrow), then you can use a VK_ constant to check. Otherwise, if it's a character (i.e. '1', 'a', 'A'), you can just test for the character value.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  5. #5
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,659
    This thread needs to be on the Windows board or the Game board...

    ...and yes, you could use an array of bitmaps. One way is to loop forward through the array until the end is reached, at which point the direction is reversed.


    Code:
    char direction = 1;
    int MAX = num_frames;
    int MIN = -1;
    int index = MIN+1;
    int end = MAX;
    
    while(running) {
     
        Show(image[index]); 
        
        index += direction; 
    
         if(index == end) {
          direction = -direction;
            if(end == MAX) {
               end = MIN, index = MAX-1;
            } else {
               end = MAX, index = min+1;
            }
        }
     }

    ...ok, well something like that...



    ITSA
    Socket Library!

  6. #6
    Registered User SPiRiToFCaT's Avatar
    Join Date
    May 2002
    Posts
    35
    If you have the images stored in an array from say element 0 to element 9, then if you have a counter that just increments every time, simply use the modulo (%) operator to keep it in the 0 to 9 range.
    i++;
    i %= 10; // or i = i % 10;
    - Well that's my 4c Australian.

  7. #7
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Hmm, but what about when the counter loops back to -13491824125 or whatever it is? Could that screw up the count?
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  8. #8
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145

    Re: "walking" animation

    Originally posted by Leeman_s
    Say I have one big bitmap with all the diff. pictures for a guy walking, or i had all diff. bitmaps for it. Either way, I'm wondering if this would work:

    I could declare an integer whose value is 10, and decrement it each time the user hits the key that moves the guy. then when i draw the guy to the screen, i check where the int is at and then put it in a switch statement and depending what it is it will display one of the states of the walking position. and when it gets to zero, reset it to 10. (assuming there are 10 diff walking positions). is this a way to do it or is there something more efficient?
    Depends on how complex animations you have. I usually have an integer holding which animation frame it is, then using an algorithm to get the proper picture from the bitmap, ie:
    Code:
    RECT r;
    r.x = AnimationFrame * PLAYER_WIDTH;
    r.y = AnimationNumber * PLAYER_HEIGHT;
    r.width = PLAYER_WIDTH;
    r.height = PLAYER_HEIGHT;
    Where AnimationFrame is which frame is currently showing (in the animation) and AnimationNumber is the current animation (ie: Stand, Walk, Jump, etc...) and r is the part of the bitmap that will be painted.

    The bitmap is made so that each animation type (stand, jump...) has their own row, and on each row there are several frames to that animation.
    This way, you don't need to use switch:es, thus making your code nicer.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Opengl walking leg animation
    By Bobby230 in forum C Programming
    Replies: 3
    Last Post: 03-05-2006, 02:41 PM
  2. Threads in MFC
    By VirtualAce in forum Windows Programming
    Replies: 4
    Last Post: 12-28-2005, 05:03 PM
  3. Animation class not working
    By VirtualAce in forum Game Programming
    Replies: 5
    Last Post: 03-02-2005, 05:48 AM
  4. 3D animation (difficult?)
    By maes in forum Game Programming
    Replies: 3
    Last Post: 09-08-2003, 10:44 PM
  5. Trouble with animation using Sleep
    By QuestionC in forum Windows Programming
    Replies: 6
    Last Post: 02-06-2002, 10:08 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21