Sprite kit NSN通知在场景中不工作
我正在写一个精灵套件游戏,我的游戏中有应用内购买。我在中实现 文件IAPHelper.m中的应用程序购买(IAPHelper.m是NSObject的子类),如果 购买成功后,它会发布一个通知,代码如下:Sprite kit NSN通知在场景中不工作,sprite-kit,nsnotification,skscene,Sprite Kit,Nsnotification,Skscene,我正在写一个精灵套件游戏,我的游戏中有应用内购买。我在中实现 文件IAPHelper.m中的应用程序购买(IAPHelper.m是NSObject的子类),如果 购买成功后,它会发布一个通知,代码如下: - (void)completeTransaction:(SKPaymentTransaction *)transaction { NSLog(@"completeTransaction..."); [self provideContentForProductIdentifier:transa
- (void)completeTransaction:(SKPaymentTransaction *)transaction {
NSLog(@"completeTransaction...");
[self provideContentForProductIdentifier:transaction.payment.productIdentifier];
}
- (void)provideContentForProductIdentifier:(NSString *)productIdentifier {
NSLog(@"post a notification");
[[NSNotificationCenter defaultCenter] postNotificationName:IAPHelperProductPurchasedNotification object:productIdentifier userInfo:nil];
}
在MyScene.m(MyScene.m是SKScene的一个子类)中,我为通知添加了一个观察者:
-(id)initWithSize:(CGSize)size {
if (self = [super initWithSize:size]) {
/* Setup your scene here */
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(ok:) name:IAPHelperProductPurchasedNotification object:nil];
}
return self;
}
- (void)ok:(NSNotification *)notification {
NSLog(@"Ok, I got it");
}
我想做的是IAPHelper.m发布通知,MyScene.m接收并回复通知。但是结果是MyScene.m似乎没有收到通知,因此也没有收到代码
- (void)ok:(NSNotification *)notification {
NSLog(@"Ok, I got it");
}
没有人打电话。但是,如果我将观察者放在我的ViewController.m(UIViewController的子类)中,它就会工作。我的问题是:skscene能否接收来自任何对象的通知?如果可以,如何实施
如果有人能帮我解决这个问题,非常感谢。您的代码格式不正确,“它不工作”是一个非常模糊的问题陈述。试着解释一下你做了什么,你期望得到什么,你实际得到了什么。谢谢你的回复。我已经重新编辑了我的代码和我的问题,如果你能再看一遍,非常感谢。