更新时间:2023-11-17 16:31:22
self.view
是一个 UIView
对象。简单地转换指针不会使它成为不同类型的对象。
SKView * spriteView =(SKView *)self.view;
spriteView
现在指向对象, self.view
指向编译器,它实际指向 SKView
类型的对象。这就是为什么你没有得到任何编译器错误或警告。在运行时,您发送的消息无法由 spriteView
处理,因为它仍然只是一个 UIView
对象。 / p>
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];
Joe
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.