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
Don't forget add SpriteKit.framework under Linked Frameworks and Libraries.
Cheers, Jesse
Before casting your UIView to SKView , you must make sure to set your custom class as SKView in your storyboard.
If you notice the first view controller comes with these imports in the header file.
#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
self.view
is aUIView
object. Simply casting the pointer does not make it a different type of object.spriteView
is now pointing to the object, thatself.view
is pointing to, telling the compiler, that it actually points to an object of typeSKView
. That's why you don't get any compiler errors or warnings. At runtime you are sending messages that can't be handled byspriteView
because it is still just aUIView
object.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.
@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