roundbg.physicsBody = SKPhysicsBody(circleOfRadius: round.frame.width/2)
roundbg.physicsBody?.dynamic = false
试图将球圈入圆圈内(roundbg)。问题是,当我使用circleOfRadius作为圆形纹理时,它需要整个圆而不仅仅是边框。我明白为什么circleOfRadius正在这样做,但无法弄清楚如何抓住边界。有任何想法吗?因此理想情况下,当球落下时,它会被困在圆圈内(圆形)。
首款iOS应用/游戏,对此非常新。谢谢你的帮助!
答案 0 :(得分:6)
SKPhysicsBody(circleOfRadius:)
创建基于体积的正文。在您的情况下,您需要一个基于边缘的正文用于较大的圆圈。请尝试以下代码。
let largeCircleRadius : CGFloat = 100
var largeCirclePath = CGPathCreateMutable();
CGPathAddArc(largeCirclePath, nil, 0.0, 0.0, largeCircleRadius, 0.0, CGFloat(2.0 * M_PI), true)
let largeCircle = SKShapeNode(circleOfRadius: 100)
largeCircle.position = CGPointMake(CGRectGetWidth(frame) / 2.0, CGRectGetHeight(frame) / 2.0)
largeCircle.physicsBody = SKPhysicsBody(edgeLoopFromPath: largeCirclePath)
addChild(largeCircle)
let smallCircle = SKShapeNode(circleOfRadius: 10)
smallCircle.position = CGPointMake(CGRectGetWidth(frame) / 2.0, CGRectGetHeight(frame) / 2.0)
smallCircle.physicsBody = SKPhysicsBody(circleOfRadius: 10)
addChild(smallCircle)
在此处详细了解基于Edge和基于音量的正文: https://developer.apple.com/library/ios/documentation/GraphicsAnimation/Conceptual/SpriteKit_PG/Physics/Physics.html#//apple_ref/doc/uid/TP40013043-CH6-SW3
要创建圆的一小部分,您可以相应地更改circlePath的startAngle和endAngle。也可以使用edgeChainFromPath
代替edgeLoopFromPath
来关闭循环。
let largeCircleRadius : CGFloat = 100
var largeCirclePath = CGPathCreateMutable();
let startAngle : CGFloat = 3.14
let endAngle : CGFloat = 2 * 3.14
CGPathAddArc(largeCirclePath, nil, 0.0, 0.0, largeCircleRadius, startAngle, endAngle , false)
let largeCircle = SKShapeNode(path: largeCirclePath)
largeCircle.position = CGPointMake(CGRectGetWidth(frame) / 2.0, CGRectGetHeight(frame) / 2.0)
largeCircle.physicsBody = SKPhysicsBody(edgeChainFromPath: largeCirclePath)
addChild(largeCircle)
let smallCircle = SKShapeNode(circleOfRadius: 10)
smallCircle.position = CGPointMake(CGRectGetWidth(frame) / 2.0, CGRectGetHeight(frame) / 2.0)
smallCircle.physicsBody = SKPhysicsBody(circleOfRadius: 10)
addChild(smallCircle)