macos - NSNotificationCenter 与 SpriteKit

标签 macos sprite-kit nsnotificationcenter

我正在使用 NSNotificationCenter 来通知按下键盘上的按键时。在场景之间移动时,如果在按下导致场景转换的键后太快按下附加键,则应用程序会崩溃。我不确定是否是前一个场景不再接收通知,或者是否未设置下一个场景的通知观察者。我可以做什么来阻止这种情况发生?以下是两个不同场景的代码以及处理通知的自定义 View 。本质上,我在 CustomSKView 中发布有关按键的通知,然后使用名为 keyPressed: 的方法(此处未列出)处理相应场景中的按键操作。

LevelSelectScene.m

@implementation LevelSelectScene

-(void)didMoveToView:(SKView *)view {
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyPressed:) name:@"KeyPressedNotificationKey" object:nil];

    //perform scene setup here
    ...
}

-(void)willMoveFromView:(SKView *)view {
    [[NSNotificationCenter defaultCenter] removeObserver:self
                                                name:@"KeyPressedNotificationKey"
                                              object:nil];

    //perform additional cleanup before moving to next scene
    ...

}

菜单.m

-(void) didMoveToView:(SKView *)view {
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyPressed:) name:@"KeyPressedNotificationKey" object:nil];

    //perform menu setup here
    ...
}


-(void) willMoveFromView:(SKView *)view {
    [[NSNotificationCenter defaultCenter] removeObserver:self
                                                name:@"KeyPressedNotificationKey"
                                              object:nil];

    //perform additional cleanup before moving to next scene
    ...
}

CustomSKView.m

#import "CustomSKView.h"

@implementation CustomSKView:SKView {

}

- (id) initWithCoder:(NSCoder *)coder {
    self = [super initWithCoder:coder];
    return self;
}

- (void) keyDown:(NSEvent *)theEvent {
    [[NSNotificationCenter defaultCenter] postNotificationName:@"KeyPressedNotificationKey"
                                                        object:nil
                                                      userInfo:@{@"keyCode" : @(theEvent.keyCode)}];
}

@end

编辑:堆栈跟踪

2015-08-15 05:47:08.199 PianoKeyboardTest[21854:4643404] -[NSPathStore2 keyPressed:]: unrecognized selector sent to instance 0x10050d110
2015-08-15 05:47:08.199 PianoKeyboardTest[21854:4643404] -[NSPathStore2 keyPressed:]: unrecognized selector sent to instance 0x10050d110
2015-08-15 05:47:08.200 PianoKeyboardTest[21854:4643404] (
0   CoreFoundation                      0x00007fff8575803c __exceptionPreprocess + 172
1   libobjc.A.dylib                     0x00007fff9227376e objc_exception_throw + 43
2   CoreFoundation                      0x00007fff8575b0ad -[NSObject(NSObject) doesNotRecognizeSelector:] + 205
3   CoreFoundation                      0x00007fff856a0e24 ___forwarding___ + 1028
4   CoreFoundation                      0x00007fff856a0998 _CF_forwarding_prep_0 + 120
5   CoreFoundation                      0x00007fff8571445c __CFNOTIFICATIONCENTER_IS_CALLING_OUT_TO_AN_OBSERVER__ + 12
6   CoreFoundation                      0x00007fff85604634 _CFXNotificationPost + 3140
7   Foundation                          0x00007fff83e8e9d1 -[NSNotificationCenter postNotificationName:object:userInfo:] + 66
8   PianoKeyboardTest                   0x000000010001d50e -[CustomSKView keyDown:] + 270
9   AppKit                              0x00007fff8ba1c11b -[NSWindow _reallySendEvent:isDelayedEvent:] + 5452
10  AppKit                              0x00007fff8b3add76 -[NSWindow sendEvent:] + 470
11  AppKit                              0x00007fff8b3aa9b1 -[NSApplication sendEvent:] + 4199
12  AppKit                              0x00007fff8b2d3c68 -[NSApplication run] + 711
13  AppKit                              0x00007fff8b250354 NSApplicationMain + 1832
14  PianoKeyboardTest                   0x0000000100005322 main + 34
15  libdyld.dylib                       0x00007fff8f1ee5c9 start + 1
)

编辑:解决方案

以下是我对 CustomSKView 所做的更改。

#import "CustomSKView.h"

@implementation CustomSKView:SKView {
    // Add instance variables here

}

- (id) initWithCoder:(NSCoder *)coder {
    self = [super initWithCoder:coder];
    if (self) {
        // Allocate and initialize your instance variables here

    }
    return self;
}

- (void) keyDown:(NSEvent *)theEvent {
    [[NSNotificationCenter defaultCenter] postNotificationName:@"KeyPressedNotificationKey"
                                                        object:nil
                                                      userInfo:@{@"keyCode" : @(theEvent.keyCode)}];
}

//overridden version of SKScene's presentScene: transition: method
-(void) presentScene:(SKScene *)scene transition:(SKTransition *)transition {
    [[NSNotificationCenter defaultCenter] removeObserver:self.scene
                                                    name:@"KeyPressedNotificationKey"
                                                  object:nil];
    [super presentScene:scene transition:transition];
}

@end

最佳答案

要在游戏转换到新场景时从当前 SKScene 中删除观察者,请重写自定义 View 类中的 presentScene 方法,删除观察者,然后调用父类(super class)的 presentScene:

- (void) presentScene:(SKScene *)scene transition:(SKTransition *)transition {
    [[NSNotificationCenter defaultCenter] removeObserver:self.scene
                                                    name:@"KeyPressedNotificationKey"
                                                  object:nil];
    [super presentScene:scene transition:transition];
 }

关于macos - NSNotificationCenter 与 SpriteKit,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32023322/

相关文章:

ios - 带 Spritekit 的 Admob

objective-c - 在 UIViewController 中向 NSNotificationCenter 添加和删除观察者

ios - UIView 子类未收到 NSNotificationCenter 通知

swift - 尝试预览文件时无法让 QuickLook 工作

python - pip无法在Mac上的Docker容器内安装软件包

java - 帮助更改 OSX Eclipse Java 堆分配

objective-c - Mac 应用内购买 - 接收商店套件响应不同的线程

swift - :texture yields empty result 中的 SKTexture

ios - 为 iPhone/iPad 应用程序/游戏创建图像/ Sprite 的正确步骤是什么?

objective-c - 未调用 NSNotificationCenter 选择器