setting a bool while using an NSMutableSet

moon_magic
Unregistered
 
Post: #1
hi there. this is my first post. i've set up my key handling like in the cocoadev.com wiki. (http://www.cocoadev.com/index.pl?GameKeyboardHandling)

it's working very well except that i need to have each key press set a boolean (e.g., i want NSUpArrowFunctionKey to set UpArrowFunctionKey = true when it is down, and set false when it is up). i'm having trouble knowing where to put the code. here's what i have:

Code:
- (void) keyDown:(NSEvent *) theEvent
{
    if (!keysPressed)
    {
        keysPressed = [[NSMutableSet alloc] init];
    }
    NSNumber * keyHit = [NSNumber numberWithUnsignedInt:
        [[theEvent characters] characterAtIndex:0]];
    switch ([keyHit unsignedIntValue])
    {
        case NSUpArrowFunctionKey:
        case NSDownArrowFunctionKey:
        case NSRightArrowFunctionKey:
        case NSLeftArrowFunctionKey:
        [keysPressed addObject:keyHit];
        break;
        default:
        break;
    }
}

- (void)keyUp:(NSEvent *) theEvent
{
    if (!keysPressed)
    {
    keysPressed = [[NSMutableSet alloc] init];
    }
    NSNumber * keyReleased = [NSNumber numberWithUnsignedInt:
        [[theEvent characters] characterAtIndex:0]];
    switch ([keyReleased unsignedIntValue])
    {
        case NSUpArrowFunctionKey:
        case NSDownArrowFunctionKey:
        case NSRightArrowFunctionKey:
        case NSLeftArrowFunctionKey:
        [keysPressed removeObject:keyReleased];
        break;
        default:
        break;
    }
}

- (void)processKeys
{
    if ([keysPressed count] != 0)
    {
        NSEnumerator * enumerator = [keysPressed objectEnumerator];
        NSNumber * keyHit;
        /* process all keys of interest that are held down */
        while (keyHit = [enumerator nextObject])
        {
            switch ([keyHit unsignedIntValue])
            {
                case NSUpArrowFunctionKey:
                [ self increasePlayerYPos ];
                break;
                
                case NSDownArrowFunctionKey:
                [ self decreasePlayerYPos ];
                break;

                case NSRightArrowFunctionKey:
                [ self increasePlayerXPos ];
                break;

                case NSLeftArrowFunctionKey:
                [ self decreasePlayerXPos ];
                break;
            }
        }
sorry, that seems like a lot of code. i'm trying to add something like this:

Code:
if (UpArrowFunctionKey = false)
    {
        if (yPlayerLeftLeg < 0.0f)
        {
            yPlayerLeftLeg += 0.005f;
        }
        if (yPlayerRightLeg < 0.0f)
        {
            yPlayerRightLeg += 0.005f;
        }
    }
because i'm just doing my animations by moving vertices around, and i need a way to trigger an animation when the user isn't pressing a key. i tried using an "else" after [keysPressed count] != 0 to just use the animation if no keys were pressed, but i realized that i needed to capture specific key ups, if that makes any sense, to account for the user moving up but not moving sideways.

ack, verbose. thanks for reading.
Quote this message in a reply
Sage
Posts: 1,403
Joined: 2005.07
Post: #2
have another set thats updated in the process keys loop
Code:
process_keys {
if(last_key_state && ! current_key_state) {
   // keyup
}
last_key_state = current_key_state;
}

also if (UpArrowFunctionKey = false) is hopefully a typo, and should be ==

Sir, e^iπ + 1 = 0, hence God exists; reply!
Quote this message in a reply
moon_magic
Unregistered
 
Post: #3
thanks for the help! i'll work on that in the next few days. classes intruding on programming time right now.
Quote this message in a reply
moon_magic
Unregistered
 
Post: #4
thanks again for the help. i created another set and used it to call my animate methods; everything is working great.
Quote this message in a reply
Post Reply