我正在使用NSNotificationCenter来通知键盘上的按键。在场景之间移动时,如果在按下导致场景转换的键后按下其他键太快,应用程序将崩溃。我不确定是不是之前的场景不再接收通知,或者是否未设置下一个场景的通知观察者。我该怎么做才能阻止这种情况发生?这是两个不同场景的代码和处理通知的自定义视图。基本上,我在keyPressed:
发布了关键按下的通知,然后我在一个名为@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
...
的方法中处理相应场景中的按下,这里没有列出。
LevelSelectScene.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
...
}
}
Menu.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
CustomSKView.m
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
)
编辑:堆栈跟踪
#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
编辑:解决方案
以下是我对CustomSKView所做的更改。
#include <iostream>
using namespace std;
class Base
{};
class Derived : public Base
{};
ostream& operator <<(ostream& os, const Base& base)
{
os << "Base";
return os;
}
ostream& operator <<(ostream& os, const Derived& derived)
{
os << "Derived";
return os;
}
int main() {
Base* derived = new Derived();
cout << *derived;
delete derived;
return 0;
}
答案 0 :(得分:1)
问题似乎是时机问题。您需要在转换开始之前删除通知,但是SKView
不能为此提供方便的挂钩。
管理此方法的一种可能方法是将SKView
子类化,以提供添加和删除观察者的机制,并可以使用多个通知;一个用于击键,另一个用于转换开始/结束。当触发转换开始通知时,此子类将删除击键观察者。转换完成后,可以通知它重新观察击键。然而,这听起来很复杂。