SpriteKit Weird 'PKPhysicsBody' Crash

泪湿孤枕 提交于 2019-12-05 09:34:06

Internally, Sprite Kit has custom classes for many of the public classes. In this case SKPhysicsBody actually creates an instance of the internal class PKPhysicsBody. Presumably PK stands for "Physics Kit".

That's why your category won't work, and if I remember correctly casting won't do the trick either because SKPhysicsBody does not inherit from PKPhysicsBody, but in case I'm wrong you can try it this way:

((SKPhysicsBody)hero.physicsBody).perfectBouncing = YES;

In any case a category isn't needed here. Instead you can achieve the same effect with a helper class and class methods, for instance:

@implementation SKPhysicsHelper

+(BOOL) perfectBouncingWithBody:(SKPhysicsBody*)body {
    return (body.restitution == 1 && body.linearDamping == 0 && body.friction == 0);
}

+(void) setPerfectBouncing:(BOOL)perfectBouncing body:(SKPhysicsBody*)body {
    body.restitution = perfectBouncing;
    body.linearDamping = !perfectBouncing;
    body.friction = !perfectBouncing;
}

@end
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!