我正在创建一个应用程序,其中不同颜色的球落下,你必须通过旋转圆圈来抓住它们,以便圆圈那部分的颜色与球匹配。我遇到的一个问题是,我不知道如何仅为圆的一部分制作物理体,而不是整圆。什么是实现这一目标的最佳方式。谢谢,任何帮助表示赞赏。
func dropBall() {
let ball = SKShapeNode(circleOfRadius: 40)
ball.fillColor = colors[Int(arc4random_uniform(UInt32(level)))]
ball.strokeColor = ball.fillColor
ball.position = CGPoint(x: 0, y: self.size.height / 2.1)
addChild(ball)
let ballBody = SKPhysicsBody(circleOfRadius: 30)
ballBody.mass = 1.5
ballBody.categoryBitMask = PhysicsCategory.ball
ballBody.collisionBitMask = 4
ball.physicsBody = ballBody
mostRecentBallColor = ball.fillColor
}
func addCircleObstacle() {
var degreesSectionAngle = 360 / (level)
var radiansSectionAngle = Double(degreesSectionAngle) * (M_PI / 180.0)
var secondXCoordinate = cos(radiansSectionAngle) * 40
var secondYCoordinate = sin(radiansSectionAngle) * 40
let path = UIBezierPath()
path.addArc(withCenter: CGPoint.zero,
radius: 160,
startAngle: CGFloat(radiansSectionAngle),
endAngle: CGFloat(0),
clockwise: false)
path.addArc(withCenter: CGPoint.zero,
radius: 200,
startAngle: CGFloat(0.0),
endAngle: CGFloat(radiansSectionAngle),
clockwise: true)
circleball = obstacleByDuplicatingPath(path, clockwise: true)
obstacles.append(circleball)
circleball.position = CGPoint(x: 0, y: self.size.height / -6)
addChild(circleball)
print(level)
print(degreesSectionAngle)
print(radiansSectionAngle)
circleBody.isDynamic = false
circleBody.categoryBitMask = PhysicsCategory.Edge
circleball.physicsBody = circleBody
}
func obstacleByDuplicatingPath(_ path: UIBezierPath, clockwise: Bool) -> SKNode {
let container = SKNode()
var degreesSectionAngle = 360 / level
var radiansSectionAngle = Double(degreesSectionAngle) * (M_PI / 180.0)
var rotationFactor = CGFloat(radiansSectionAngle)
if !clockwise {
rotationFactor *= -1
}
for i in 0...level - 1 {
if i == mostRecentBallColor {
}
section = SKShapeNode(path: path.cgPath)
section.fillColor = colors[i]
section.strokeColor = colors[i]
section.zRotation = rotationFactor * CGFloat(i);
let sectionBody = SKPhysicsBody(polygonFrom: path.cgPath)
sectionBody.categoryBitMask = PhysicsCategory.Obstacle
sectionBody.collisionBitMask = 0
sectionBody.contactTestBitMask = PhysicsCategory.ball
sectionBody.affectedByGravity = false
section.physicsBody = sectionBody
container.addChild(section)
}
return container
}