【问题标题】:Decreasing size of CGPath with time in SpriteKit?在 SpriteKit 中随时间减小 CGPath 的大小?
【发布时间】:2015-07-13 06:35:21
【问题描述】:

我正在制作一个游戏,我希望精灵节点跟随圆周运动,并且随着时间的推移,圆圈的半径应该逐渐减小到零。我假设,这可以通过使用 CGPath() 创建一个圆形路径来完成,然后每次调用 update() 函数时将路径半径减小 1 个点。

代码如下:

func createOrbit() -> (CGPath) {
    let orbitPathOrigin = CGPoint(x: center.x , y: center.y)
    let orbitPathRect = CGRect(origin: orbitPathOrigin, size: orbitSize)
    let orbitPath = CGPathCreateWithEllipseInRect(orbitPathRect, nil)
    return orbitPath
}

如何引用每个创建的路径并减小它们的大小?

【问题讨论】:

标签: ios swift sprite-kit


【解决方案1】:

我认为最好的方法是创建一个路径并让项目像你提到的那样遵循该路径,

所以你可以让粒子跟随下面的路径

var mySatellite = SKSpriteNode(color: UIColor.redColor(), size: CGSize(width: 10, height: 10))
var spinAction = SKAction.followPath(self.createOrbit(), asOffset: false, orientToPath: true, duration: 4.0)

mySatellite.runAction(spinAction)

在您的 didMoveToView 方法或类似方法中的某处设置时间操作,

var wait = SKAction.waitForDuration(4.0)
var play = SKAction.runBlock { () -> Void in
               var newPath = self.decreasedOrbit(20.0)   
               var spinAction = SKAction.followPath(newPath, asOffset: false, orientToPath: true, duration: 4.0)
           }

var waitAndPlaySeq = SKAction.sequence([wait,play])
mySatellite.runAction(waitAndPlaySeq)

AssignNewOrbit 函数为粒子分配新路径。

decreasedOrbit 函数如下所示,仅用于创建新的小轨道。

你还需要减小每个周期的轨道大小

   func decreasedOrbit(diameter : CGFloat) -> CGPath{

       var newDiameter = CGFloat(diameter - 5.0)
       var squareRect = CGRect(origin: CGPoint(x: 0, y: 0), size: CGSize(width: newDiameter, height: newDiameter))
       var bezierPath = UIBezierPath(ovalInRect: squareRect)

       return bezierPath.CGPath
   }

希望对你有帮助。

【讨论】:

  • 这听起来很有效。让我在尝试后的几个小时内更新你。谢谢你的回答。
  • 你能不能输入你的函数 reductionOrbit( ) 的实现?
  • 我也添加了 reducedOrbit() 函数,但您可以编辑原点参数或减小尺寸(在我的示例中为 5.0)。
  • @mert,您不应该将 NSTimer 与 Sprite Kit 一起使用:stackoverflow.com/questions/23978209/spritekit-creating-a-timer
  • @ABakerSmith 我已经阅读了 NSTimer 链接,你是对的 似乎 NSTimer 与 SpriteKit 有一点问题。无论如何,我已经更新了我的答案,请看一下,感谢您的修复:)
猜你喜欢
  • 2019-11-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-05
  • 2011-12-19
  • 2016-02-20
  • 1970-01-01
相关资源
最近更新 更多