节点旋转超出角度

时间:2015-04-23 19:19:46

标签: ios swift sprite-kit

我创建了一个圆圈:

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,animated:true)

我还宣布了一个小球,它将从屏幕顶部落下并接触中心圆。游戏逻辑是匹配小球和圆圈扇区之间的颜色,如果颜色不匹配,游戏结束。 我现在面临的问题是,无论什么,圈子应该是这样的:

enter image description here

但是,如果我只是继续点击屏幕旋转圆圈并且游戏继续重启,那么圆圈将会旋转出角度,如下所示:

enter image description here

我有什么建议可以解决这个问题吗?

1 个答案:

答案 0 :(得分:0)

首先,我认为您应该使用CGFloat(M_PI)而不是3,14,因为3,14是pi的错误近似值。

其次,当您点击速度超过0.2秒时,您运行的SKAction是否可能会在未完成的旋转上运行而导致移位?我也是SKAction的新手,所以我不确定它会如何表现但是你可以尝试更慢地点击(在修复M_PI之后)并看看它的行为方式?如果问题是由于在未完成的旋转上运行第二次旋转引起的,则可以禁用第二次旋转直到旋转完成,或者您可以将其排队以在第一次旋转完成后执行。