Hi all,
I’ve just completed Section II:(Physics and Nodes/Cat Nap) and now trying to deepen my understanding by playing with “Pin Joint”.
I thought that an analog clock would be the easiest study for that purpose.
However, it’s not working so far. So, I’m not sure if my direction and approach is right…
Do you use SpriteKit when you create a simple analog clock?
My goal is just to spin the clock hand(360 degrees) in accordance with the duration I configure such as 5 sec, 10 sec, 30 sec, and 60 sec.
Code snnipet
import SpriteKit
class ClockNode: SKSpriteNode, CustomNodeEvents {
private var clockBase = SKSpriteNode(imageNamed: “base”)
private var clockHand = SKSpriteNode(imageNamed: “hand”)
private var clockJoint: SKPhysicsJointPin!
func didMoveToScene() {
guard let scene = scene else {
return
}
userInteractionEnabled = trueclockBase.anchorPoint = CGPoint(x: 0.5, y: 0.5) //center clockBase.position = position addChild(clockBase)
clockHand.anchorPoint = CGPoint(x: clockBase.position.x, y: clockBase.position.y) clockHand.position = position
let pinPoint = CGPoint(x: clockBase.position.x,y: clockBase.position.y) clockJoint = SKPhysicsJointPin.jointWithBodyA(clockBase.physicsBody!, bodyB: clockHand.parent!.physicsBody!, anchor: pinPoint) scene.physicsWorld.addJoint(clockJoint) //clockHand.physicsBody!.allowsRotation = true //clockHand.physicsBody!. addChild(clockHand) clockHand.physicsBody!.applyImpulse(CGVector(dx: 50, dy: 25))
// let fortyFive = SKAction.rotateToAngle(CGFloat(3.14), duration: NSTimeInterval(1))
// clockHand.runAction(fortyFive)
}
}
Many thanks in advance for your help.