添加SKSpriteNode
:
let container = SKSpriteNode.init(color: UIColor.redColor(), size: CGSizeMake(800, 400))
container.position=CGPointMake(300, 300)
self.addChild(container)
創建的CGPoint
的數組。我以前只是一個三角形,你可以使用任何你想要的:
let firstPathArray:[CGPoint]=[CGPoint(x: 0, y: 0),CGPoint(x: 100, y: 0),CGPoint(x: 50, y: 100)]
添加路徑容器:
addbezierPathToContainer(container,pathArray: firstPathArray)
添加路徑:
func addbezierPathToContainer(container:SKSpriteNode,pathArray:[CGPoint]) -> Void {
let newpath = UIBezierPath()
var startpoint:Bool=false;
for path in pathArray {
if(!startpoint)
{
newpath.moveToPoint(CGPoint(x: path.x, y: path.y))
startpoint=true;
}
else
{
newpath.addLineToPoint(CGPoint(x: path.x, y: path.y))
newpath.addLineToPoint(CGPoint(x: path.x, y: path.y))
}
}
//close path
newpath.closePath()
UIColor.blackColor().setStroke()
newpath.stroke()
let pathNode = SKShapeNode(path: newpath.CGPath)
container.addChild(pathNode);
}
非常感謝你。 – Mamazur