我想为我的 SpriteKit 游戏支持键盘,但问题是:
我们从 ViewController 获取命令键:
- (NSArray *)keyCommands {
return @[[UIKeyCommand keyCommandWithInput:@" " modifierFlags:0 action:@selector(fire)]];
}
但是游戏逻辑都在SKScene中,从ViewController中呈现出来……而且有多个SKScene……场景如何从ViewController中获取命令呢?或者我们需要进行键盘轮询?
最佳答案
您可以通过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];
关于ios - 从 SKScene 获取命令键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58377053/