Search code examples
iosswiftsprite-kitsknode

SKAction.rotate not running on SKCameraNode


So the actual SKCameraNode is working fine, since I make it follow another node and that works just fine. But when I try to rotate the camera with an SKAction, it won't rotate. When I try to set its zRotation though, it works fine. I don't really get why.

Here are some excerpts.

import SpriteKit

class Field:SKScene {
  let screen = UIScreen.main.bounds
  var cam = SKCameraNode()
  var ball:SKShapeNode = SKShapeNode()

  override func didMove(to view: SKView) {
    // ball init and other stuff
    scene!.camera = cam
  }

  func followBall() {
    cam.position.x = ball.position.x
    cam.position.y = ball.position.y+100
  }

  override func update(_ currentTime: TimeInterval) {
    followBall()
  }

  override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    for touch in touches {
      let location = touch.location(in: self.cam)
      if(location.x < screen.width/2) {
        cam.run(SKAction.rotate(toAngle: .pi/8, duration: 0.2))
      } else if (location.x > screen.width/2) {
        cam.run(SKAction.rotate(toAngle: -.pi/8, duration: 0.2))
      }
    }
  }

  override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
    cam.run(SKAction.rotate(toAngle: 0, duration: 0.2))
  }
}

Solution

  • cam wasn't added to the scene @0x141E provided the answer.