1

I am making a game in which I want a sprite node to follow a circular movement, and with time the circles radius should gradually decrease to zero. I am assuming, this can be done by making a circular path using CGPath() and then decrease the paths radius by 1 point everytime the update() function is called.

Here is the code:

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
}

How do I refer to each of the path that is created and reduce their size?

Mehul
  • 572
  • 4
  • 17
  • Are you trying to create a spiral then? In which case take a look at http://stackoverflow.com/questions/30427482/moving-a-skspritenode-in-a-downward-loop-using-swift – ABakerSmith Jul 13 '15 at 11:11

1 Answers1

1

I think best way to do is creating a path and make items follow that path like you mentioned,

So you can make particles follow path like below

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)

Set time action somewhere in your didMoveToView method or alike,

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 function to assigning new route to the particle.

decreasedOrbit function like below just for only creating new small orbits.

And also you need to decrease the size of orbit for each period

   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
   }

Hope it helps.

mert
  • 1,090
  • 13
  • 26
  • This sounds like it works. Let me update you in a few hours after trying. Thank you for answering though. – Mehul Jul 13 '15 at 08:09
  • Can you also type the implementation of your function decreaseOrbit( )? – Mehul Jul 13 '15 at 08:14
  • I've added decreasedOrbit() function too but you can edit origin parameter or decrease size (5.0 in my example). – mert Jul 13 '15 at 08:42
  • @mert, you shouldn't be using `NSTimer` with Sprite Kit: http://stackoverflow.com/questions/23978209/spritekit-creating-a-timer – ABakerSmith Jul 13 '15 at 11:54
  • @ABakerSmith I've read NSTimer link and you're right It seems that NSTimer is little buggy with SpriteKit. Anyway I've updated my answer take a look please, thanks for the fix :) – mert Jul 13 '15 at 12:58