1
votes

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?

1
Are you trying to create a spiral then? In which case take a look at stackoverflow.com/questions/30427482/…ABakerSmith

1 Answers

1
votes

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.