问题
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
回答1:
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.
回答2:
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.
回答3:
Don't forget add SpriteKit.framework under Linked Frameworks and Libraries.
Cheers, Jesse
回答4:
what you have to do is to change the subclass of view from UIView To SKView select ur viewcontroller under that view and change it subclass
回答5:
@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
回答6:
Before casting your UIView to SKView , you must make sure to set your custom class as SKView in your storyboard.
回答7:
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
来源:https://stackoverflow.com/questions/20277246/spritekit-view-throwing-nsinvalidargumentexception