简单的雪碧套件场景设置出错

我正在尝试设置一个简单的Sprite Kit安装程序。 我正在做的是从“空应用程序”重新创build默认的xCode模板“雪碧套件游戏”。

它不断崩溃在skView.showsFPS = YES; 线。 我无法解释。 你可以吗? 谢谢!

一些代码:

AppDelegate.m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions: (NSDictionary *)launchOptions { self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; MenuController *menuController = [[MenuController alloc] init]; self.window.rootViewController = menuController; [self.window makeKeyAndVisible]; return YES; } 

MenuController.m

 - (void)viewDidLoad { [super viewDidLoad]; SKView *skView = (SKView *)self.view; skView.showsFPS = YES; skView.showsNodeCount = YES; MultiplayerView *gameView = [MultiplayerView sceneWithSize:skView.bounds.size]; gameView.scaleMode = SKSceneScaleModeAspectFill; [skView presentScene:gameView]; } 

当我启动这个时,出现以下错误:

 2013-11-10 13:08:01.605 ByS[9419:70b] -[UIView setShowsFPS:]: unrecognized selector sent to instance 0x10c00bc60 2013-11-10 13:08:01.608 ByS[9419:70b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIView setShowsFPS:]: unrecognized selector sent to instance 0x10c00bc60' 

在界面生成器中,将窗口视图的类更改为SKView 。 将UIViewinputSKView将不会执行任何操作,除非UIView以前通过投射SKView获得。

从单视图或空应用程序开始,与通常的SpriteKit模板相比,我遇到同样的问题。 当我添加一些我发现覆盖loadView方法的代码时,我的工作。 尝试把它放在你的视图控制器的viewDidLoad之上。

 - (void)loadView { CGRect applicationFrame = [[UIScreen mainScreen] applicationFrame]; SKView *skView = [[SKView alloc] initWithFrame:applicationFrame]; self.view = skView; } 

.h文件是通常的UIViewController。 并且记得在链接框架后#import !:

 @interface CharacterViewController : UIViewController 

所以完整的.m代码将是这样的:

 @implementation CharacterViewController - (void)loadView { CGRect applicationFrame = [[UIScreen mainScreen] applicationFrame]; SKView *skView = [[SKView alloc] initWithFrame:applicationFrame]; self.view = skView; } - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { // Custom initialization } return self; } - (void)viewDidLoad { [super viewDidLoad]; NSLog(@"CharacterViewController viewDidLoad"); // Do any additional setup after loading the view. // Configure the view. SKView * skView = (SKView *)self.view; skView.showsFPS = YES; skView.showsNodeCount = YES; // Create and configure the scene. CharacterScene *character = [[CharacterScene alloc]initWithSize:CGSizeMake(skView.bounds.size.width, skView.bounds.size.height)]; character.backgroundColor = [UIColor blueColor]; [skView presentScene:character]; } 

或者你可以在prepareForSegue:或者在任何其他的演示之前,把它修复得很快并且很脏

 -(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{ UIViewController *destinationVC = segue.destinationViewController; SKView * skView = (SKView *)destinationVC.view; if (![skView isKindOfClass:[SKView class]]) { skView = [[SKView alloc] initWithFrame: skView.frame]; // self.view.bounds destinationVC.view = skView; } } 

在MenuController.m中replace你的初始化

SKView *skView = (SKView *) self.view;

 SKView *skView = [[SKView alloc initWithFrame : self.view.frame]; 
  • 您只需通过给self.view框架来初始化SKView的对象,而不是通过指定self.view的引用对象。