gpt4 book ai didi

swift - 填充从 CGPath 创建的 SKShapeNode

转载 作者:行者123 更新时间:2023-11-30 10:10:20 41 4
gpt4 key购买 nike

我正在尝试基于点数组创建自定义 SKShapeNode。这些点形成一个封闭的形状,最终需要填充该形状。

这是我到目前为止所想到的,但由于某种原因,笔画画得很好,但形状仍然是空的。我错过了什么?

override func didMoveToView(view: SKView)
{
let center = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame))
let path = CGPathCreateMutable()


CGPathMoveToPoint(path, nil, center.x, center.y)
CGPathAddLineToPoint(path, nil, center.x + 50, center.y + 50)

CGPathMoveToPoint(path, nil, center.x + 50, center.y + 50)
CGPathAddLineToPoint(path, nil, center.x - 50, center.y + 50)

CGPathMoveToPoint(path, nil, center.x - 50, center.y + 50)
CGPathAddLineToPoint(path, nil, center.x - 50, center.y - 50)

CGPathMoveToPoint(path, nil, center.x - 50, center.y - 50)
CGPathAddLineToPoint(path, nil, center.x, center.y)

CGPathCloseSubpath(path)

let shape = SKShapeNode(path: path)
shape.strokeColor = SKColor.blueColor()
shape.fillColor = SKColor.redColor()
self.addChild(shape)
}

最佳答案

您的路径有问题。您通常会调用 CGPathMoveToPoint 来设置路径的起点,然后调用一系列 CGPathAdd* 来将线段添加到路径中。尝试像这样创建它:

let path = CGPathCreateMutable()         
CGPathMoveToPoint(path, nil, center.x, center.y)
CGPathAddLineToPoint(path, nil, center.x + 50, center.y + 50)
CGPathAddLineToPoint(path, nil, center.x - 50, center.y + 50)
CGPathAddLineToPoint(path, nil, center.x - 50, center.y - 50)
CGPathCloseSubpath(path)

阅读CGPath Reference (搜索CGPathMoveToPoint)了解更多详细信息。

关于swift - 填充从 CGPath 创建的 SKShapeNode,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33300223/

41 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com