SpriteKit view throwing NSInvalidArgumentException

2020-07-27 06:04发布

I have this code below that loads a scene in the viewDidLoad: method.

SKView *spriteView = (SKView *) self.view;
spriteView.showsDrawCount = YES;
spriteView.showsNodeCount = YES;
spriteView.showsFPS = YES;
PFPiePlanesScene *scene = [PFPiePlanesScene sceneWithSize:self.view.frame.size];
scene.scaleMode = SKSceneScaleModeAspectFill;
[spriteView presentScene:scene];

When I call anything on the spriteView, it crashes with this message:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIView setShowsDrawCount:]: unrecognized selector sent to instance 0x9685030'

I assume this is because it is not treating the instance as a SKView and instead as a UIView.

Thanks in advance.

Some extra:

Sprite kit uses this code to load a scene.

// Configure the view.
SKView * skView = (SKView *)self.view;
skView.showsFPS = NO;
skView.showsNodeCount = NO;

// Create and configure the scene.
self.scene = [SKMyScene sceneWithSize:skView.bounds.size];
self.scene.scaleMode = SKSceneScaleModeAspectFill;

// Present the scene.
[skView presentScene:self.scene];

And, with no error. The classes are the exact same. Would just coming into the view screw this up?

Joe

7条回答
该账号已被封号
2楼-- · 2020-07-27 06:26

Don't forget add SpriteKit.framework under Linked Frameworks and Libraries.

Cheers, Jesse

查看更多
萌系小妹纸
3楼-- · 2020-07-27 06:30

Before casting your UIView to SKView , you must make sure to set your custom class as SKView in your storyboard.

查看更多
叛逆
4楼-- · 2020-07-27 06:32

If you notice the first view controller comes with these imports in the header file.

 #import <UIKit/UIKit.h>

#import <SpriteKit/SpriteKit.h>

When you create a new view controller, by default Spritekit is not included. After adding sprite kit it should work with the default code apple provides

查看更多
仙女界的扛把子
5楼-- · 2020-07-27 06:36

self.view is a UIView object. Simply casting the pointer does not make it a different type of object.

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

spriteView is now pointing to the object, that self.view is pointing to, telling the compiler, that it actually points to an object of type SKView. That's why you don't get any compiler errors or warnings. At runtime you are sending messages that can't be handled by spriteView because it is still just a UIView object.

查看更多
Emotional °昔
6楼-- · 2020-07-27 06:41

If you are following apple's walk-thougrh you skipped the part : "Open the storyboard for the project. It has a single view controller (SpriteViewController). Select the view controller’s view object and change its class to SKView.

查看更多
姐就是有狂的资本
7楼-- · 2020-07-27 06:49

@Sebastian is not correct.

i guess you are following apple's walk-thougrh. maybe the following tutorial will help:

http://www.raywenderlich.com/49625/sprite-kit-tutorial-space-shooter

查看更多
登录 后发表回答