SpriteKit很奇怪'PKPhysicsBody'崩溃 [英] SpriteKit Weird 'PKPhysicsBody' Crash

查看:150
本文介绍了SpriteKit很奇怪'PKPhysicsBody'崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为 SKPhysicsBody 实现一个简单的类别,这将允许我将完美弹跳应用于物理身体。

I am trying to implement a simple category for SKPhysicsBody that will allow me to apply 'perfect bouncing' to a physics body.

这将只是当 perfectBouncing = YES 时会发生以下情况:

What this will simply do is when perfectBouncing = YES the following will happen:

restitution = 1.0

linearDamping = 0.0

friction = 0.0

然而,当我尝试使用我的时候实现如下所示,我得到一个奇怪的崩溃告诉我,我无法将 perfectBouncing 消息发送到 PKPhysicsBody ,是一个我从未参考过的类,在我的项目中或我在SpriteKit SDK中的知识中不存在。

However, when I try to use my implementation as shown below, I get a weird crash telling me that I can't send perfectBouncing messages to PKPhysicsBody, which is a class that I never refer to and doesn't exist in my project or to my knowledge in the SpriteKit SDK.

是的,正在导入类别。

崩溃消息

-[PKPhysicsBody setPerfectBouncing:]: unrecognized selector sent to instance 0x7fa828f84610

*** First throw call stack:
(
    0   CoreFoundation                      0x000000010882b3f5 __exceptionPreprocess + 165
    1   libobjc.A.dylib                     0x0000000107dbfbb7 objc_exception_throw + 45
    2   CoreFoundation                      0x000000010883250d -[NSObject(NSObject) doesNotRecognizeSelector:] + 205
    3   CoreFoundation                      0x000000010878a7fc ___forwarding___ + 988
    4   CoreFoundation                      0x000000010878a398 _CF_forwarding_prep_0 + 120
    5   Squash The Dot                      0x000000010785662c -[gameNode viewDidAnimateIn] + 1116
    6   Squash The Dot                      0x000000010783e709 -[lzyViewHandler initViewChild:] + 3305
    7   Squash The Dot                      0x000000010783d93d -[lzyViewHandler initViewChildren:] + 573
    8   Squash The Dot                      0x000000010785d3e0 __28-[splashVC viewDidAnimateIn]_block_invoke + 256
    9   SpriteKit                           0x000000010933837e _ZN9SKCAction25didFinishWithTargetAtTimeEP9SKCSprited + 44
    10  SpriteKit                           0x0000000109399638 _ZN9SKCSprite6updateEd + 170
    11  SpriteKit                           0x0000000109352dd4 -[SKScene _update:] + 119
    12  SpriteKit                           0x000000010936d299 -[SKView(Private) _update:] + 563
    13  SpriteKit                           0x000000010936ac2f -[SKView renderCallback:] + 829
    14  SpriteKit                           0x0000000109367cec __29-[SKView setUpRenderCallback]_block_invoke + 54
    15  SpriteKit                           0x0000000109394744 -[SKDisplayLink _callbackForNextFrame:] + 256
    16  QuartzCore                          0x00000001091f88f7 _ZN2CA7Display15DisplayLinkItem8dispatchEv + 37
    17  QuartzCore                          0x00000001091f87bf _ZN2CA7Display11DisplayLink14dispatch_itemsEyyy + 315
    18  CoreFoundation                      0x00000001087934e4 __CFRUNLOOP_IS_CALLING_OUT_TO_A_TIMER_CALLBACK_FUNCTION__ + 20
    19  CoreFoundation                      0x00000001087930a5 __CFRunLoopDoTimer + 1045
    20  CoreFoundation                      0x00000001087563dd __CFRunLoopRun + 1901
    21  CoreFoundation                      0x0000000108755a06 CFRunLoopRunSpecific + 470
    22  GraphicsServices                    0x000000010be319f0 GSEventRunModal + 161
    23  UIKit                               0x000000010953f550 UIApplicationMain + 1282
    24  Squash The Dot                      0x00000001078550a3 main + 115
    25  libdyld.dylib                       0x000000010b6cb145 start + 1
)

类别实施

#import <SpriteKit/SpriteKit.h>

@interface SKPhysicsBody (lzySKPhysics)

// Enables perfect boucing by setting friction = 0, linearDamping = 0 and restitution = 1.
@property (nonatomic) BOOL perfectBouncing;

@end


@implementation SKPhysicsBody (lzySKPhysics)

-(BOOL) perfectBouncing {
    return (self.restitution == 1 && self.linearDamping == 0 && self.friction == 0);
}

-(void) setPerfectBouncing:(BOOL)perfectBouncing {
    self.restitution = perfectBouncing;
    self.linearDamping = !perfectBouncing;
    self.friction = !perfectBouncing;
}

@end

用法示例

hero = [[SKSpriteNode alloc] initWithColor:[SKColor blueColor] size:CGSizeMake(heroRadius*2, heroRadius*2)];

hero.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:heroRadius];
hero.physicsBody.perfectBouncing = YES;

任何想法这个难以捉摸的 PKPhysicsBody 是和为什么我遇到这个崩溃?

Any ideas what this elusive PKPhysicsBody is and why I am having this crash?

推荐答案

在内部,Sprite Kit有许多公共类的自定义类。在这种情况下,SKPhysicsBody实际上创建了内部类PKPhysicsBody的实例。推测PK代表物理套件。

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".

这就是为什么你的类别不起作用的原因,如果我记得正确的铸造也不会成功,因为SKPhysicsBody没有继承自PKPhysicsBody,但如果我错了你可以这样试试:

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

这篇关于SpriteKit很奇怪'PKPhysicsBody'崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆