【问题标题】:Posting NSNotification from object inside SKScene error从 SKScene 错误中的对象发布 NSNotification
【发布时间】:2014-09-11 10:52:15
【问题描述】:

我有由 ViewController 控制的 SKScene。在这个 SKScene 中,我有一个带有一些程序的棋盘对象(从 NSObject 派生)。我需要从其中一个程序(游戏板程序)中发布通知。在 ViewController 我有一个观察者:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(rowBangEmitAtPosition:) name:@"rowBangN" object:nil];

在游戏板的程序里面我有:

[[NSNotificationCenter defaultCenter] postNotificationName:@"rowBangN" object:self userInfo:@{@"position":point}];

对象是 SKScene 的一部分(作为变量)。但是这个通知永远不会被触发。谁能帮帮我。

【问题讨论】:

  • 发布您的 rowBangEmitAtPosition: 实现以及调用方法的顺序。含义:确保您在发布通知之前注册了观察者。
  • 我在 -(void)viewDidLoad 期间注册了观察者,并在 -(void)viewWillLayoutSubviews 期间初始化场景和对象,所以我认为没问题。
  • 您是否尝试过使用 NSLog 或断点来验证该方法没有被调用?
  • 是的,断点和NSLog都有,但是没有触发。

标签: sprite-kit nsnotifications


【解决方案1】:

我想通了——当我发布代码 object:nil 而不是 object:self 时,它正在工作。我不知道为什么,但它有效:)。 谢谢大家。

【讨论】:

    猜你喜欢
    • 2013-10-06
    • 1970-01-01
    • 1970-01-01
    • 2023-03-19
    • 2015-11-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多