正如您在下面的屏幕截图中看到的,圆(SKShapeNode)和线(也是 SKShapeNode)之间有很大的差距。
自然,这两个项目在碰撞时都应该接触。
这是我的代码:
- (void)createSceneContents
{
[self setBackgroundColor:[SKColor blackColor]];
// Floor
//
SKShapeNode *floorShapeNode = [SKShapeNode node];
CGFloat threeQuartersDown = CGRectGetMidY(self.frame) * 0.5f;
CGPoint floorFromPoint = CGPointMake(CGRectGetMinX(self.frame), threeQuartersDown);
CGPoint floorToPoint = CGPointMake(CGRectGetMaxX(self.frame), threeQuartersDown);
CGMutablePathRef floorLinePath = CGPathCreateMutable();
CGPathMoveToPoint(floorLinePath, NULL, floorFromPoint.x, floorFromPoint.y);
CGPathAddLineToPoint(floorLinePath, NULL, floorToPoint.x, floorToPoint.y);
[floorShapeNode setPath:floorLinePath];
[floorShapeNode setPhysicsBody:[SKPhysicsBody bodyWithEdgeFromPoint:floorFromPoint toPoint:floorToPoint]];
[floorShapeNode setStrokeColor:[SKColor whiteColor]];
[floorShapeNode setLineWidth:0.5f];
[self addChild:floorShapeNode];
// Player
//
[self setPlayerNode:[BZPlayerNode node]];
[self.playerNode setPhysicsBody:[SKPhysicsBody bodyWithCircleOfRadius:(CGRectGetWidth(self.playerNode.frame) / 2.0f)]];
[self.playerNode.physicsBody setDynamic:YES];
[self.playerNode setPosition:CGPointMake(CGRectGetMidX(self.frame) - CGRectGetMidX(self.playerNode.frame), CGRectGetMidY(self.frame) - CGRectGetMidX(self.playerNode.frame) + 100.0f)];
[self addChild:self.playerNode];
}
以及 SKShapeNode 的 +node 方法:
+ (instancetype)node
{
BZPlayerNode *playerNode = [super node];
if (playerNode) {
[playerNode setFillColor:[SKColor whiteColor]];
[playerNode setPath:CGPathCreateWithEllipseInRect(CGRectMake(0.0f, 0.0f, 80.0f, 80.0f), NULL)];
}
return playerNode;
}
有谁知道我哪里出错了?
谢谢