【问题标题】:Get command key from SKScene从 SKScene 获取命令键
【发布时间】:2019-10-14 12:40:01
【问题描述】:

我想为我的 SpriteKit 游戏支持键盘,但问题是:

我们从 ViewController 获取命令键:

- (NSArray *)keyCommands {
    return @[[UIKeyCommand keyCommandWithInput:@" " modifierFlags:0 action:@selector(fire)]];
}

但是游戏逻辑都在SKScene中,从ViewController呈现出来……而且还有多个SKScene……场景如何从ViewController获取命令?还是我们需要进行键盘轮询?

【问题讨论】:

  • 当你说多个场景时......我希望你的意思是你有可以随时呈现的不同类,而不是一次显示多个场景
  • 是的,通常一个游戏都有一个场景,每个场景用于标题、游戏玩法、游戏结束等。
  • 好的,那么你想要一个协议,什么时候。我有时间我会写一个答案

标签: ios objective-c sprite-kit


【解决方案1】:

您可以通过NSNotification发送消息

SKScene中添加观察者,

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

发布通知表单UIViewController

NSDictionary *userInfo = @{ @"Key": @"Value" };
[[NSNotificationCenter defaultCenter] postNotificationName: @"NOTIFICATION_NAME" object:nil userInfo:userInfo];

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-17
    • 2014-01-03
    相关资源
    最近更新 更多