节点旋转出角度
Node rotation out of angle
我创建了一个圈子:
class FourColorCircle : SKShapeNode {
override init() {
super.init()
self.createCircle()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
func createCircle () {
let center = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame))
// node1
let node1bezierPath = UIBezierPath()
node1bezierPath.addArcWithCenter(center, radius: 100, startAngle: 0.78, endAngle: 2.35, clockwise: true)
node1bezierPath.addLineToPoint(center)
let node1 = SKShapeNode(path: node1bezierPath.CGPath)
node1.strokeColor = SKColor.redColor()
node1.fillColor = SKColor.redColor()
node1.physicsBody = SKPhysicsBody(polygonFromPath: node1bezierPath.CGPath)
node1.physicsBody?.dynamic = false
node1.physicsBody?.affectedByGravity = false
node1.physicsBody?.categoryBitMask = contactBodies.redNode.rawValue
node1.physicsBody?.contactTestBitMask = ballGroup.redBall.rawValue | ballGroup.blueBall.rawValue | ballGroup.greenBall.rawValue | ballGroup.yellowBall.rawValue
self.addChild(node1)
// node2
let node2bezierPath = UIBezierPath()
node2bezierPath.addArcWithCenter(center, radius: 100, startAngle: 2.35, endAngle: 3.92, clockwise: true)
node2bezierPath.addLineToPoint(center)
let node2 = SKShapeNode(path: node2bezierPath.CGPath)
node2.strokeColor = SKColor.blueColor()
node2.fillColor = SKColor.blueColor()
node2.physicsBody = SKPhysicsBody(polygonFromPath: node2bezierPath.CGPath)
node2.physicsBody?.dynamic = false
node2.physicsBody?.affectedByGravity = false
node2.physicsBody?.categoryBitMask = contactBodies.blueNode.rawValue
node2.physicsBody?.contactTestBitMask = ballGroup.redBall.rawValue | ballGroup.blueBall.rawValue | ballGroup.greenBall.rawValue | ballGroup.yellowBall.rawValue
self.addChild(node2)
// node3
let node3bezierPath = UIBezierPath()
node3bezierPath.addArcWithCenter(center, radius: 100, startAngle: 3.92, endAngle: 5.48, clockwise: true)
node3bezierPath.addLineToPoint(center)
let node3 = SKShapeNode(path: node3bezierPath.CGPath)
node3.strokeColor = SKColor.greenColor()
node3.fillColor = SKColor.greenColor()
node3.physicsBody = SKPhysicsBody(polygonFromPath: node3bezierPath.CGPath)
node3.physicsBody?.dynamic = false
node3.physicsBody?.affectedByGravity = false
node3.physicsBody?.categoryBitMask = contactBodies.greenNode.rawValue
node3.physicsBody?.contactTestBitMask = ballGroup.redBall.rawValue | ballGroup.blueBall.rawValue | ballGroup.greenBall.rawValue | ballGroup.yellowBall.rawValue
self.addChild(node3)
// node4
let node4bezierPath = UIBezierPath()
node4bezierPath.addArcWithCenter(center, radius: 100, startAngle: 5.48, endAngle: 0.78, clockwise: true)
node4bezierPath.addLineToPoint(center)
let node4 = SKShapeNode(path: node4bezierPath.CGPath)
node4.strokeColor = SKColor.yellowColor()
node4.fillColor = SKColor.yellowColor()
node4.physicsBody = SKPhysicsBody(polygonFromPath: node4bezierPath.CGPath)
node4.physicsBody?.dynamic = false
node4.physicsBody?.affectedByGravity = false
node4.physicsBody?.categoryBitMask = contactBodies.yellowNode.rawValue
node4.physicsBody?.contactTestBitMask = ballGroup.redBall.rawValue | ballGroup.blueBall.rawValue | ballGroup.greenBall.rawValue | ballGroup.yellowBall.rawValue
self.addChild(node4)
}
func rotate(angle : CGFloat, animated : Bool) {
var rotateAction : SKAction!
if animated {
rotateAction = SKAction.rotateByAngle(angle, duration: 0.2)
}
else {
rotateAction = SKAction.rotateByAngle(angle, duration: 0)
}
self.runAction(rotateAction)
}
}
我设置为每次用户点击屏幕时旋转 90 度:
centerCircle.rotate(-3.14/2, 动画: true)
我还声明了一个小球,它会从屏幕顶部掉落并接触到中心圆。游戏逻辑是匹配小球和圆圈的扇形之间的颜色,如果颜色不匹配,则游戏结束。
我现在的问题是,无论如何,圆应该是这样的:
但是当我一直点着屏幕旋转圆圈,游戏不断重启时,圆圈会旋转出角度,像这样:
有什么解决办法的建议吗?
首先,我认为您应该使用 CGFloat(M_PI)
而不是 3,14
,因为 3,14
不是圆周率的近似值。
其次,当您敲击速度超过 0.2 秒时,是否有可能 SKAction
您 运行 是 运行 未完成的旋转导致移位?我也是 SKAction
的新手,所以我不确定它会如何表现,但您可以尝试更慢地点击(在修复 M_PI
之后)并查看它的表现吗?如果问题是由 运行 在未完成的旋转上进行第二次旋转引起的,您可以禁用第二次点击直到旋转完成,或者您可以将其排队等待在第一次旋转完成后执行。
我创建了一个圈子:
class FourColorCircle : SKShapeNode {
override init() {
super.init()
self.createCircle()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
func createCircle () {
let center = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame))
// node1
let node1bezierPath = UIBezierPath()
node1bezierPath.addArcWithCenter(center, radius: 100, startAngle: 0.78, endAngle: 2.35, clockwise: true)
node1bezierPath.addLineToPoint(center)
let node1 = SKShapeNode(path: node1bezierPath.CGPath)
node1.strokeColor = SKColor.redColor()
node1.fillColor = SKColor.redColor()
node1.physicsBody = SKPhysicsBody(polygonFromPath: node1bezierPath.CGPath)
node1.physicsBody?.dynamic = false
node1.physicsBody?.affectedByGravity = false
node1.physicsBody?.categoryBitMask = contactBodies.redNode.rawValue
node1.physicsBody?.contactTestBitMask = ballGroup.redBall.rawValue | ballGroup.blueBall.rawValue | ballGroup.greenBall.rawValue | ballGroup.yellowBall.rawValue
self.addChild(node1)
// node2
let node2bezierPath = UIBezierPath()
node2bezierPath.addArcWithCenter(center, radius: 100, startAngle: 2.35, endAngle: 3.92, clockwise: true)
node2bezierPath.addLineToPoint(center)
let node2 = SKShapeNode(path: node2bezierPath.CGPath)
node2.strokeColor = SKColor.blueColor()
node2.fillColor = SKColor.blueColor()
node2.physicsBody = SKPhysicsBody(polygonFromPath: node2bezierPath.CGPath)
node2.physicsBody?.dynamic = false
node2.physicsBody?.affectedByGravity = false
node2.physicsBody?.categoryBitMask = contactBodies.blueNode.rawValue
node2.physicsBody?.contactTestBitMask = ballGroup.redBall.rawValue | ballGroup.blueBall.rawValue | ballGroup.greenBall.rawValue | ballGroup.yellowBall.rawValue
self.addChild(node2)
// node3
let node3bezierPath = UIBezierPath()
node3bezierPath.addArcWithCenter(center, radius: 100, startAngle: 3.92, endAngle: 5.48, clockwise: true)
node3bezierPath.addLineToPoint(center)
let node3 = SKShapeNode(path: node3bezierPath.CGPath)
node3.strokeColor = SKColor.greenColor()
node3.fillColor = SKColor.greenColor()
node3.physicsBody = SKPhysicsBody(polygonFromPath: node3bezierPath.CGPath)
node3.physicsBody?.dynamic = false
node3.physicsBody?.affectedByGravity = false
node3.physicsBody?.categoryBitMask = contactBodies.greenNode.rawValue
node3.physicsBody?.contactTestBitMask = ballGroup.redBall.rawValue | ballGroup.blueBall.rawValue | ballGroup.greenBall.rawValue | ballGroup.yellowBall.rawValue
self.addChild(node3)
// node4
let node4bezierPath = UIBezierPath()
node4bezierPath.addArcWithCenter(center, radius: 100, startAngle: 5.48, endAngle: 0.78, clockwise: true)
node4bezierPath.addLineToPoint(center)
let node4 = SKShapeNode(path: node4bezierPath.CGPath)
node4.strokeColor = SKColor.yellowColor()
node4.fillColor = SKColor.yellowColor()
node4.physicsBody = SKPhysicsBody(polygonFromPath: node4bezierPath.CGPath)
node4.physicsBody?.dynamic = false
node4.physicsBody?.affectedByGravity = false
node4.physicsBody?.categoryBitMask = contactBodies.yellowNode.rawValue
node4.physicsBody?.contactTestBitMask = ballGroup.redBall.rawValue | ballGroup.blueBall.rawValue | ballGroup.greenBall.rawValue | ballGroup.yellowBall.rawValue
self.addChild(node4)
}
func rotate(angle : CGFloat, animated : Bool) {
var rotateAction : SKAction!
if animated {
rotateAction = SKAction.rotateByAngle(angle, duration: 0.2)
}
else {
rotateAction = SKAction.rotateByAngle(angle, duration: 0)
}
self.runAction(rotateAction)
}
}
我设置为每次用户点击屏幕时旋转 90 度:
centerCircle.rotate(-3.14/2, 动画: true)
我还声明了一个小球,它会从屏幕顶部掉落并接触到中心圆。游戏逻辑是匹配小球和圆圈的扇形之间的颜色,如果颜色不匹配,则游戏结束。 我现在的问题是,无论如何,圆应该是这样的:
但是当我一直点着屏幕旋转圆圈,游戏不断重启时,圆圈会旋转出角度,像这样:
有什么解决办法的建议吗?
首先,我认为您应该使用 CGFloat(M_PI)
而不是 3,14
,因为 3,14
不是圆周率的近似值。
其次,当您敲击速度超过 0.2 秒时,是否有可能 SKAction
您 运行 是 运行 未完成的旋转导致移位?我也是 SKAction
的新手,所以我不确定它会如何表现,但您可以尝试更慢地点击(在修复 M_PI
之后)并查看它的表现吗?如果问题是由 运行 在未完成的旋转上进行第二次旋转引起的,您可以禁用第二次点击直到旋转完成,或者您可以将其排队等待在第一次旋转完成后执行。