Sprite Kit failing assertion: (typeA == b2_dynamicBody || typeB == b2_dynamicBody)

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-29 15:00:06

You can change the dynamic property back and forth, however, you should be very careful if you're using collision/contact detection.

During collision/contact between two bodies, it is asserted that at least one of them must be a dynamic body. If you change that property to NO at runtime, and the other contacted body is also static (non-dynamic), and the collision/contact callback is happening, it will throw this particular assertion error - contact will be detected between 2 static bodies mid-execution, which Box2d doesn't enjoy.

A possible workaround trick - works for me, even directly in the didBeginContact: method:

SKPhysicsBody *temp = node.physicsBody;
temp.dynamic = NO;
node.physicsBody = temp;

If the original poster's intent is to disable collisions while directly manipulating an SKNode, it can be done as follows:

@property (nonatomic, strong) SKNode *node;
@property (nonatomic, assign) uint32_t previousCollisionBitMask;

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
  // Disable collisions & gravity
  self.previousCollisionBitMask = self.node.physicsBody.collisionBitMask;
  self.node.physicsBody.collisionBitMask = 0;
  self.node.physicsBody.affectedByGravity = NO;
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
  // Enable collisions & gravity
  self.node.physicsBody.collisionBitMask = self.previousCollisionBitMask;
  self.node.physicsBody.affectedByGravity = YES;
}

This takes the object out of calculation for collisions. Disabling gravity keeps the object from falling while you're moving it.

If you're also concerned about the contactTextBitMask, you can use similar logic to disable that while you're manipulating the scene with touches.

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