沿轴旋转精灵

Rotate a sprite along an axis

在 SpriteKit 中,我需要沿着一个轴(例如,穿过精灵中心的轴)旋转一个精灵,就像一个由用户旋转的轮子。

我尝试使用 applyTorque 函数(施加仅 angular 而不是线性的力),但我无法处理屏幕上不同运动引起的不同力(更长的触摸屏幕,施加更大的力量)。

有人可以帮助我了解如何处理这个问题吗?

这是顺时针或逆时针旋转轮子的基本示例,具体取决于您是按屏幕的左侧还是右侧。按住以提高速度:

class GameScene : SKScene {

  enum Direction { case left, right }

  var directionToMove: Direction?

  let wheel = SKShapeNode(circleOfRadius: 25)
  let speedLabel = SKLabelNode(text: "Speed: 0")

  override func didMove(to view: SKView) {
    // Scene setup:
    anchorPoint = CGPoint(x: 0.5, y: 0.5)
    removeAllChildren()

    physicsBody = SKPhysicsBody(edgeLoopFrom: frame)

    wheel.fillColor = .blue
    wheel.physicsBody = SKPhysicsBody(circleOfRadius: 25)
    wheel.physicsBody!.affectedByGravity = false
    let wheelDot = SKSpriteNode(color: .gray, size: CGSize(width: 5, height:5))
    wheel.addChild(wheelDot)
    wheelDot.position.y += 20
    wheel.setScale(3)


    speedLabel.setScale(3)
    speedLabel.position.y = (frame.maxY - speedLabel.frame.size.height / 2) - 45

    addChild(wheel)
    addChild(speedLabel)
  }

  // Change this to touchesBegan for iOS:
  override func mouseDown(with event: NSEvent) {
    // Change this to touches.first!.location(in: self) for iOS.
    let location = event.location(in: self)

    // Determine if touch left or right side:
    if location.x > 0 {
     directionToMove = .right
    }
    else if location.x < 0 {
      directionToMove = .left
    }
  }

  override func mouseUp(with event: NSEvent) {
    // Stop applying gas:
    directionToMove = nil
    print("lol")
  }

  override func update(_ currentTime: TimeInterval) {
    // This is how much speed we gain each frame:
    let torque = CGFloat(0.01)

    guard let direction = directionToMove else { return }

    // Apply torque in the proper direction
    switch direction {
    case .left:
      wheel.physicsBody!.applyTorque(torque)
    case .right:
      wheel.physicsBody!.applyTorque(-torque)
    }
  }

  override func didSimulatePhysics() {
    // Speedometer:
    speedLabel.text = "Speed: \(abs(Int(wheel.physicsBody!.angularVelocity)))"
  }
}

这是一个根据您向左/向右滑动的速度旋转球的答案:

class GameScene: SKScene {

  let speedLabel = SKLabelNode(text: "Speed: 0")
  let wheel      = SKShapeNode(circleOfRadius: 25)

  var recognizer:  UIPanGestureRecognizer!

  func pan(recognizer: UIPanGestureRecognizer) {
    let velocity = recognizer.velocity(in: view!).x

    // Play with this value until if feels right to you.
    let adjustor = CGFloat(60)

    let speed = velocity / adjustor
    wheel.physicsBody!.angularVelocity = -speed
  }

  // Scene setup:
  override func didMove(to view: SKView) {
    removeAllChildren()

    physicsBody = SKPhysicsBody(edgeLoopFrom: frame)

    wheel.fillColor = .blue
    wheel.physicsBody = SKPhysicsBody(circleOfRadius: 25)
    wheel.physicsBody!.affectedByGravity = false
    let wheelDot = SKSpriteNode(color: .gray, size: CGSize(width: 5, height:5))
    wheel.addChild(wheelDot)
    wheelDot.position.y += 20
    wheel.setScale(3)

    speedLabel.setScale(3)
    speedLabel.position.y = (frame.maxY - speedLabel.frame.size.height / 2) - 45

    recognizer = UIPanGestureRecognizer(target: self, action: #selector(pan))
    view.addGestureRecognizer(recognizer)

    addChild(wheel)
    addChild(speedLabel)
  }

  override func didSimulatePhysics() {
    speedLabel.text = "Speed: \(abs(Int(wheel.physicsBody!.angularVelocity)))"
  }
}