f_qi
f_qi

Reputation: 699

Node rotation out of angle

I created a circle by:

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)
    }
}

I set it to rotate by 90 degree every time when the user taps the screen:

centerCircle.rotate(-3.14/2, animated: true)

I also declared a small ball, which will fall from top of screen and contact the center circle. The game logic is to match the color between the small ball and the sector of the circle, if the color doesn't match, game over. The issue I have right now is that, no matter what, the circle should be like this:

enter image description here

But when if I just keep tap on the screen to rotate the circle and the game keeps restart, the circle will rotate out of angle, like this:

enter image description here

Any suggestions how I can fix this?

Upvotes: 0

Views: 93

Answers (1)

mrtksn
mrtksn

Reputation: 422

First off, I think you should use CGFloat(M_PI) instead of 3,14 because 3,14 is a bad approximation of the pi.

Secondly, is it possible that when you tap faster than 0.2 seconds the SKAction you run is run over uncompleted rotation causing a shift? I'm also newbie with the SKAction, so I'm not sure how it would behave but could you try to tap more slowly (after fixing the M_PI) and see how it behaves? If the problem is caused by running a second rotation over uncompleted rotation you can disable the second tap until the rotation is complete or you can queue it to execute after the first rotation is completed.

Upvotes: 0

Related Questions