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

查看:24
本文介绍了如何在 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天全站免登陆