0

I'm learning how to use SpriteKit and would like to have a circle that:

  • Grows and shrinks smoothly
  • Pulses with a colour
  • Is displayed with crisp edges

So far, I've come up with the following code:

import SpriteKit

class GameScene: SKScene {

    override func didMoveToView(view: SKView) {
        addCircle()
    }

    func addCircle(){
        let circle = SKShapeNode(circleOfRadius: 50 ) // Size of Circle
        circle.position = CGPointMake(frame.midX, frame.midY)  //Middle of Screen
        circle.glowWidth = 0.0 // No border
        circle.fillColor = SKColor.yellowColor() // Start as yellow
        let actualDuration = 1 // Animate for 1s
        // Basic actions for grow, shrink, colour up, colour down
        let actionGrow = SKAction.scaleTo(CGFloat(2), duration: NSTimeInterval(actualDuration))
        actionGrow.timingMode = SKActionTimingMode.EaseInEaseOut
        let actionShrink = SKAction.scaleTo(CGFloat(0.5), duration: NSTimeInterval(actualDuration))
        actionShrink.timingMode = SKActionTimingMode.EaseInEaseOut
        let actionColorUp = SKAction.colorizeWithColor(UIColor.redColor(), colorBlendFactor: 1.0, duration: NSTimeInterval(actualDuration))
        let actionColorDown = SKAction.colorizeWithColor(UIColor.redColor(), colorBlendFactor: 0.0, duration: NSTimeInterval(actualDuration))
        // Combine the actions
        let actionGrowWithColor = SKAction.group([actionGrow,actionColorUp])
        let actionShrinkWithColor = SKAction.group([actionShrink,actionColorDown])
        // Run and repeat
        circle.runAction(SKAction.repeatActionForever(SKAction.sequence([actionGrowWithColor,actionShrinkWithColor])))
        // Add the circle
        self.addChild(circle)

    }
}

The first of my three criteria are met, but the other two are not.

  • As the SKShapeNode is not a vector, as it grows the edges are not crisp. Is there a better way to draw a circle, or should I just start with a circle that is sufficiently large?
  • Is there a reason why the colorizeWithColor section doesn't appear to have any effect?

Many thanks in advance!

Ben
  • 4,707
  • 5
  • 34
  • 55
  • 1
    You can't animate shape's `fillColor` like that. `colorizeWithColor` animates color property (and colorBlendFactor) of a sprite. SKShapeNode doesn't have those properties. You could use custom action with duration to animate fillColor and a strokeColor properties. – Whirlwind Mar 26 '16 at 18:40
  • Thanks @Whirlwind, am I right in thinking that I'll need to use `SKAction.customActionWithDuration`, and then work out a new colour for each time the block gets evaluated, based on `elapsedTime`? – Ben Mar 26 '16 at 18:56
  • 1
    Correct. You can start [here](http://stackoverflow.com/a/27952397/3402095) – Whirlwind Mar 26 '16 at 18:57

0 Answers0