当SKPhysicsBody.node->时出现Swift ClassCastException。 SKLabelNode。为什么? [英] Swift ClassCastException when SKPhysicsBody.node -> SKLabelNode. Why?

查看:124
本文介绍了当SKPhysicsBody.node->时出现Swift ClassCastException。 SKLabelNode。为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码导致类强制转换异常。有人可以解释我为什么吗?

The following code results to class cast exception. Could someone explain me why?

var firstBody: SKPhysicsBody
var secondBody: SKPhysicsBody
firstBody = contact.bodyA
secondBody = contact.bodyB
projectileDidCollide(firstBody.node as SKSpriteNode, flyingLabel: secondBody.node as SKLabelNode)

导致ClassCastException:

results in ClassCastException:

libswiftCore.dylib`swift_dynamicCastObjCClassUnconditional:
0x104668980:pushq%rbp
0x104668981:movq %rsp,%rbp
0x104668984:pushq%rbx
0x104668985:pushq%rax
0x104668986:movq%rsi,%rcx
0x104668989:movq%rdi,%rbx
0x10466898c:xorl%eax,%eax
0x10466898e:testq%rbx,%rbx
0x104668991:je 0x1046689ac; swift_dynamicCastObjCClassUnconditional + 44
0x104668993:movq 0x7f236(%rip),%rsi; isKindOfClass:
0x10466899a:movq%rbx,%rdi
0x10466899d:movq%rcx,%rdx
0x1046689a0:callq 0x10466b46a;符号存根为:objc_msg发送
0x1046689a5:testb%al,%al
0x1046689a7:movq%rbx,%rax
0x1046689aa:je 0x1046689b3; swift_dynamicCastObjCClassUnconditional + 51
0x1046689ac:addq $ 0x8,%rsp
0x1046689b0:popq%rbx
0x1046689b1:popq%rbp
0x1046689b2:retq

0x1046 0xc158(%rip),%rax; 快速动态转换失败
0x1046689ba:movq%rax,0x87427(%rip); gCRAnnotations + 8
0x1046689c1:int3

0x1046689c2:nopw%cs:(%rax,%rax)

libswiftCore.dylib`swift_dynamicCastObjCClassUnconditional: 0x104668980: pushq %rbp 0x104668981: movq %rsp, %rbp 0x104668984: pushq %rbx 0x104668985: pushq %rax 0x104668986: movq %rsi, %rcx 0x104668989: movq %rdi, %rbx 0x10466898c: xorl %eax, %eax 0x10466898e: testq %rbx, %rbx 0x104668991: je 0x1046689ac ; swift_dynamicCastObjCClassUnconditional + 44 0x104668993: movq 0x7f236(%rip), %rsi ; "isKindOfClass:" 0x10466899a: movq %rbx, %rdi 0x10466899d: movq %rcx, %rdx 0x1046689a0: callq 0x10466b46a ; symbol stub for: objc_msgSend 0x1046689a5: testb %al, %al 0x1046689a7: movq %rbx, %rax 0x1046689aa: je 0x1046689b3 ; swift_dynamicCastObjCClassUnconditional + 51 0x1046689ac: addq $0x8, %rsp 0x1046689b0: popq %rbx 0x1046689b1: popq %rbp 0x1046689b2: retq
0x1046689b3: leaq 0xc158(%rip), %rax ; "Swift dynamic cast failed" 0x1046689ba: movq %rax, 0x87427(%rip) ; gCRAnnotations + 8 0x1046689c1: int3
0x1046689c2: nopw %cs:(%rax,%rax)

推荐答案

您不知道联系人处理程序中的哪个主体-对于任何给定的联系人,SpriteKit都将两个主体标记为 bodyA bodyB 以任意顺序。即使您设置了接触位掩码以使所有冲突都在一个精灵和标签之间, bodyA 可能是一个冲突中的精灵,而标签是另一个冲突。

You don't know which body is which in your contact handler — for any given contact, SpriteKit labels the two bodies bodyA and bodyB in arbitrary order. Even if your contact bit masks are set up so that all collisions are between a sprite and a label, bodyA may be the sprite on one collision and the label on another.

您的联系人处理程序应对此进行说明。在移交给需要了解每个主体类型的代码之前,请检查主体的任何可能顺序。您可以通过 as?进行有条件的强制转换,或者(在更具冲突性的游戏中更有用)检查 categoryBitMask

Your contact handler should account for this. Check either possible order of bodies before handing off to code that needs to know the type of each. You can do this by conditional casting with as? or (to be more generally useful in games with more kinds of collisions) checking the categoryBitMask of each body.

这篇关于当SKPhysicsBody.node->时出现Swift ClassCastException。 SKLabelNode。为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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