问题描述
我正在尝试为节点创建CGPath,但是当我尝试使用608_hd_best_practices_for_building_spritekit_games的动作和常量幻灯片中定义的SKShapeNode时,出现错误调用中的额外参数'count'.有什么想法吗?
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 */
}
}
推荐答案
init(splinePoints:count:)
以UnsafeMutablePointer<CGPoint>
(即,一个由CGPoint
s组成的C数组)和一个无符号整数作为其参数.它还返回SKShapeNode
对象而不是CGPathRef
.尝试以下操作...
init(splinePoints:count:)
takes an UnsafeMutablePointer<CGPoint>
(i.e., a C array of CGPoint
s) 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))
也进行以下更改
let speed:CGFloat = 1.0
let action = SKAction.followPath(p.path, speed: speed)
sprite.runAction(action)
这篇关于Swift SKShapeNode shapeWithSplinePoints的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!