PDA

View Full Version : setting a bool while using an NSMutableSet


moon_magic
2006.05.15, 01:11 AM
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:

- (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:

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.

unknown
2006.05.15, 01:15 AM
have another set thats updated in the process keys loop
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 ==

moon_magic
2006.05.15, 08:44 PM
thanks for the help! i'll work on that in the next few days. classes intruding on programming time right now.

moon_magic
2006.05.24, 10:18 PM
thanks again for the help. i created another set and used it to call my animate methods; everything is working great.