以可变速度摆动箭头

时间:2018-10-20 19:21:27

标签: ios swift sprite-kit skspritenode skaction

如何使箭头像this video中那样摆动?

到目前为止,我可以像this video一样来回旋转节点,使用didMove(to:)SKScene中的以下代码:

// Ball
let ballNode = SKSpriteNode(imageNamed: "Ball")
let offsetFromCorner: CGFloat = 20
ballNode.position = CGPoint(x: frame.minX + ballNode.size.width / 2 + offsetFromCorner, y: frame.minY + ballNode.size.height / 2 + offsetFromCorner)
addChild(ballNode)

/* ... */

// Aim arrow
let aimArrowNode = SKSpriteNode(imageNamed: "AimArrow")
aimArrowNode.position.y += aimArrowNode.size.height / 2
ballNode.addChild(aimArrowNode)
ballNode.zRotation = -.pi / 18 * 8
let rotateUp = SKAction.rotate(toAngle: -.pi / 18, duration: 1)
let rotateDown = SKAction.rotate(toAngle: -.pi / 18 * 8, duration: 1)
let combinedActions = SKAction.sequence([rotateUp, rotateDown])
ballNode.run(SKAction.repeatForever(combinedActions))

但是,我希望箭头越靠近边缘,它看起来就越“慢”。我该如何实现?


如有任何疑问,请询问!

1 个答案:

答案 0 :(得分:0)

幸运的是,答案实际上内置于SKAction中。

在创建操作后只需添加以下内容:

rotateUp.timingMode = .easeInEaseOut
rotateDown.timingMode = .easeInEaseOut

现在您可以得到我一直在寻找的效果! :)