大家好我正在制作一个sprite-kit应用程序,用户必须能够绘制多行。为此,我制作了以下代码。
import SpriteKit
class GameScene: SKScene {
var line = SKShapeNode()
var path = CGPathCreateMutable()
var touch: UITouch!
var location:CGPoint!
override func didMoveToView(view: SKView) {
}
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
/* Called when a touch begins */
touch = touches.anyObject() as UITouch!
location = touch.locationInNode(self)
drawLine()
}
override func touchesEnded(touches: NSSet!, withEvent event: UIEvent!) {
self.delete(line)
}
func drawLine() {
CGPathMoveToPoint(path, nil, location.x, location.y)
CGPathAddLineToPoint(path, nil, 500.0, 500.0)
line.path = path
line.strokeColor = UIColor.redColor()
line.lineWidth = 5.0
self.addChild(line)
}
override func update(currentTime: CFTimeInterval) {
/* Called before each frame is rendered */
}
}
这样可行,但当用户尝试绘制第二行时,应用程序崩溃。 有人可以向我解释为什么会这样吗?
------ ------编辑
我想我需要使用一系列行,所以我做了类似的事情:
var line: [[SKShapeNode]] = []
这给了我以下错误: [[SKShapeNode]]没有名为'path'的成员。 所以我虽然我必须使路径变量也是一个数组,但这只给了我第二个错误:GameScene没有名为'path'的成员。所以我把它全部改回来但我仍然认为我必须使用数组,尽管我无法弄清楚如何让它工作。
答案 0 :(得分:1)
复制并粘贴此..现在,您每次都会重新定义您的行
import SpriteKit
class GameScene: SKScene {
var line = SKShapeNode()
var path = CGPathCreateMutable()
var touch: UITouch!
var location:CGPoint!
override func didMoveToView(view: SKView) {
}
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
/* Called when a touch begins */
touch = touches.anyObject() as UITouch!
location = touch.locationInNode(self)
drawLine()
}
override func touchesEnded(touches: NSSet!, withEvent event: UIEvent!) {
self.delete(line)
}
func drawLine() {
CGPathMoveToPoint(path, nil, location.x, location.y)
CGPathAddLineToPoint(path, nil, 500.0, 500.0)
SKShapeNode *line = [SKShapeNode node]; //redfine line every time
line.path = path
line.strokeColor = UIColor.redColor()
line.lineWidth = 5.0
self.addChild(line)
}
override func update(currentTime: CFTimeInterval) {
/* Called before each frame is rendered */
}
}