【问题标题】:How to rotate a sprite along a circular path?如何沿圆形路径旋转精灵?
【发布时间】:2017-07-10 11:32:00
【问题描述】:

这是一个由四个单独的精灵组成的轮子。我的目标是将它们作为轮子旋转。我已经尝试过'UIBezier' arccenter 类,但最终结果是单个精灵沿着中心移动但也旋转了它的形状,导致了不想要的结果。这是相关的代码。 'orientToPath' 当设置为 true 时,会旋转精灵本身。

//path rotation
let dx = whlPurple.position.x - self.size.width/2
let dy = whlPurple.position.y - (whlRed.size.height/2 + (whlPurple.position.y - whlRed.size.height))
let rad = atan2(dy, dx)
let radius = sqrt((dx*dx) + (dy*dy))
let path = UIBezierPath(arcCenter: CGPoint(x: self.size.width/2, y: (whlRed.size.height/2 + (whlPurple.position.y - whlRed.size.height))), radius: radius, startAngle: rad, endAngle: rad + CGFloat(M_PI * 6), clockwise: true)

let follow = SKAction.follow(path.cgPath, asOffset: false, orientToPath: true, speed: 200)

whlPurple.run(SKAction.repeatForever(follow))

【问题讨论】:

    标签: swift sprite-kit


    【解决方案1】:

    创建一个 SKNode 并将其放置在您的圆的中心。

    然后在 CGPoint(x:radius,y:0) 的位置添加你的精灵(addChild)。你可以设置 SKNode 的 zRotation 来选择你在圆上的起始位置。

    当您旋转 SKNode 时,您的精灵将在一个完美的圆圈中移动。例如,要让恶意在 5 秒内消失,您可以使用 SKAction.rotate(by: 2*pi, duration:5) 并永远重复(在包含精灵的 SKNode 上(而不是精灵本身)。

    【讨论】:

      猜你喜欢
      • 2016-09-22
      • 1970-01-01
      • 2017-10-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-22
      • 1970-01-01
      相关资源
      最近更新 更多