识别在Sprite Kit中按下的W,A,S,D空格键

时间:2014-07-14 02:35:54

标签: sprite-kit sprite

我正在制作一个SpriteKit游戏但我在识别按键何时按下时遇到了麻烦。我想特别识别密钥:W, A, S, D, spacebar, and ESC

例如:

if (w is press) {
     //jump
}

1 个答案:

答案 0 :(得分:0)

使用以下代码完成此操作:

    - (void)keyDown:(NSEvent *)event {
        [self handleKeyEvent:event keyDown:YES];
    }

    - (void)keyUp:(NSEvent *)event {
        [self handleKeyEvent:event keyDown:NO];
    }

    - (void)handleKeyEvent:(NSEvent *)event keyDown:(BOOL)downOrUp {
         NSString *characters = [event characters];
         for (int s = 0; s<[characters length]; s++) {
            unichar oneCharacter = [characters characterAtIndex:s];
            switch (oneCharacter) {
                case 'w':
                    [character jump];
                    break;
                case 'a':
                    [character left];
                    break;
                case 'd':
                    [character right];
                    break;
                case 's':
                    NSLog(@"Down");
                    break;
                case ' ':
                 NSLog(@"Shoot");
                 break;
            }
        }
    }

我想赞扬:Heyfara(他/她在另一个问题上回答了这个问题)。要查看原始帖子,请点击此处:How Can I get keyboard input in a SpriteKit Game?