0

I am trying create a CGPath for a node to follow but when I have tried using SKShapeNode as defined in the actions and constants slide from 608_hd_best_practices_for_building_spritekit_games I get the error Extra argument 'count' in call. Any thoughts?

import SpriteKit

class GameScene: SKScene {
    var groundNode: SKSpriteNode? = SKSpriteNode()
    var path = [CGPoint]()

    override func didMoveToView(view: SKView) {
        groundNode = childNodeWithName("ground") as? SKSpriteNode
    }

    override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
        path = [CGPoint]()
    }

    override func touchesMoved(touches: NSSet, withEvent event: UIEvent) {
        var newPoint = touches.anyObject()?.locationInNode(groundNode)
        path.append(newPoint!)
    }

    override func touchesEnded(touches: NSSet, withEvent event: UIEvent) {
        let sprite = SKSpriteNode(imageNamed:"Spaceship")

        sprite.xScale = 0.25
        sprite.yScale = 0.25
        sprite.position = path.first!

        groundNode?.addChild(sprite)

        // TODO: this currently isnt working
        let count = path.count
        // CGPathRef p = [SKShapeNode shapeWithSplinePoints:points]
        let p: CGPathRef = SKShapeNode(splinePoints: path, count: count)

        let speed:CGFloat = 1.0

        let action = SKAction.followPath(p, speed: speed)
        sprite.runAction(action)
    }

    override func update(currentTime: CFTimeInterval) {
        /* Called before each frame is rendered */
    }
}
CodeSmile
  • 64,284
  • 20
  • 132
  • 217
ubersnack
  • 512
  • 6
  • 19
  • path is not a CGPoint* equivalent but rather a NSArray where the CGPoint are wrapped in NSValue instances, so the type doesn't match the function definition, see https://developer.apple.com/library/prerelease/ios/documentation/SpriteKit/Reference/SKShapeNode_Ref/index.html#//apple_ref/occ/clm/SKShapeNode/shapeNodeWithPoints:count: – CodeSmile Dec 26 '14 at 18:53

1 Answers1

1

init(splinePoints:count:) takes an UnsafeMutablePointer<CGPoint> (i.e., a C array of CGPoints) and an unsigned integer as its arguments. It also returns an SKShapeNode object not a CGPathRef. Try the following...

let p = SKShapeNode(splinePoints: &path, count: UInt(count))

EDIT: Make the following changes as well

let speed:CGFloat = 1.0

let action = SKAction.followPath(p.path, speed: speed)
sprite.runAction(action)
0x141E
  • 12,613
  • 2
  • 41
  • 54
  • See my updated answer. Are you planning to show the path that the sprite is following? – 0x141E Dec 27 '14 at 19:29
  • I wasn't planning on showing the path at this moment, just using the drawn out path to move the sprite along (the SKShapeNode could just be added to the scene along with setting the fillColor if I did want to) - thanks for the help :) – ubersnack Dec 28 '14 at 00:25