如何在SpriteKit游戏中获得键盘输入? [英] How Can I get keyboard input in a SpriteKit Game?

查看:179
本文介绍了如何在SpriteKit游戏中获得键盘输入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是SpriteKit编程的初学者,并且一直在尝试弄清楚如何处理键盘输入.

I'm a beginner in SpriteKit programming, and have been trying to figure out how to handle input from the keyboard.

到目前为止,我发现您应该子类化NSResponder并像这样实现它:

What I've found so far is that you should subclass NSResponder and implement it like this:

@interface AppDelegate : NSResponder <NSApplicationDelegate>
-(void)keyUp:(NSEvent *)theEvent;
-(void)keyDown:(NSEvent *)theEvent;
@end

@implementation AppDelegate
-(void)keyUp:(NSEvent *)theEvent
{
   NSLog(@"Key Released");
}
-(void)keyDown:(NSEvent *)theEvent
{
  NSLog(@"Key Pressed");
}
@end

很明显,AppDelegate的界面和实现中还有更多方法/属性,但我没有将它们放在此处以保持相关性.

Obviously, there are a few more methods/properties in the interface and implementation of AppDelegate but I didn't put them there to keep the question relevant.

接下来,我将开始使用键代码来检测正在按下的键,但是keyUpkeyDown方法甚至都不会被调用.我不确定为什么.

Next, I would start using key codes to detect which keys are being pressed, but the keyUp and keyDown methods don't even get called. I'm not sure why.

有帮助吗?

更新: 感谢您的回答!我发现您必须直接在场景类中实现keyUpkeyDown,因为不会从AppDelegate调用它们.再次感谢您的帮助!

Update: Thanks for your answers! I discovered that you have to implement keyUp and keyDown directly in your scene class, because they won't get called from the AppDelegate. Thanks again for the help!

推荐答案

我知道的最简单的方法是在SKScene中实现keyDown方法(而不是直接在AppDelegate中实现).您不必继承任何子类.

The easiest way I know is to implement the keyDown method in your SKScene (and not directly in the AppDelegate). You don't have to subclass anything.

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

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

然后使用方法handleKeyEvent:(NSEvent *)event keyDown:(BOOL)downOrUp检查已按下哪个键:

Then use the method handleKeyEvent:(NSEvent *)event keyDown:(BOOL)downOrUp to check which key has been pressed :

- (void)handleKeyEvent:(NSEvent *)event keyDown:(BOOL)downOrUp {
    // First check the arrow keys since they are on the numeric keypad.
    if ([event modifierFlags] & NSNumericPadKeyMask) { // arrow keys have this mask
        NSString *theArrow = [event charactersIgnoringModifiers];
        unichar keyChar = 0;
        if ([theArrow length] == 1) {
            keyChar = [theArrow characterAtIndex:0];
            switch (keyChar) {
                case NSUpArrowFunctionKey:
                    self.defaultPlayer.moveForward = downOrUp;
                    break;
                case NSLeftArrowFunctionKey:
                    self.defaultPlayer.moveLeft = downOrUp;
                    break;
                case NSRightArrowFunctionKey:
                    self.defaultPlayer.moveRight = downOrUp;
                    break;
                case NSDownArrowFunctionKey:
                    self.defaultPlayer.moveBack = downOrUp;
                    break;
            }
        }
    }

    // Now check the rest of the keyboard
    NSString *characters = [event characters];
    for (int s = 0; s<[characters length]; s++) {
        unichar character = [characters characterAtIndex:s];
        switch (character) {
            case 'w':
                self.defaultPlayer.moveForward = downOrUp;
                break;
            case 'a':
                self.defaultPlayer.moveLeft = downOrUp;
                break;
            case 'd':
                self.defaultPlayer.moveRight = downOrUp;
                break;
            case 's':
                self.defaultPlayer.moveBack = downOrUp;
                break;
            case ' ':
                self.defaultPlayer.fireAction = downOrUp;
                break;
        }
    }
}

我从Apple SpriteKit Adventure游戏中获取了此代码.我发现学习SpriteKit非常有用:)

I took this code from the Apple SpriteKit Adventure game. I found it very usefull to learn SpriteKit :)

这篇关于如何在SpriteKit游戏中获得键盘输入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆