圓角矩形我有下面的代碼舍入從現場建設者已經存在的矩形...與SKShapeNode
btOk.path = UIBezierPath(roundedRect: CGRect(x: -62.5,y:-25,width: 125, height: 50), cornerRadius: 10).cgPath
但是,如果我嘗試使用相同的初始化到一輪又矩形的角是它更大,它甚至沒有接近工作。它只是寬度HUUUUGE(想象特朗普)。
scene.enumerateChildNodes(withName: "//*"){
node,stop in
if let name = node.name{
if name.contains("round"){
if let shapeNode = node as? SKShapeNode{
print(shapeNode.frame.width) //500
shapeNode.path = UIBezierPath(roundedRect: CGRect(x: -250,y:-100,width: 500, height: 200), cornerRadius: 50).cgPath
print(shapeNode.frame.width) //5735
}
}
}
}
btOk
也是SKShapeNode。我在兩個如此不同的人之間缺少什麼?有一點需要注意的是,我正在通過這樣的場景的孩子來枚舉,因爲這個場景在SKReferenceNode中。也許這跟它有關係?
編輯
從@Ron Myschuk考慮的方向,我已經解決了這一點,因爲它的這種皮塔餅,我還創建了一個擴展。所以現在我可以在需要時非常容易地圍繞任何SKShapeNode的角落。即使它是在場景編輯器中創建的。請注意,只有在形狀節點沒有子節點的情況下才能使用。否則,這些孩子也將被刪除。
extension SKShapeNode {
func roundCorners(topLeft:Bool,topRight:Bool,bottomLeft:Bool,bottomRight:Bool,radius: CGFloat,parent: SKNode){
let newNode = SKShapeNode(rect: self.frame)
newNode.fillColor = self.fillColor
newNode.lineWidth = self.lineWidth
newNode.position = self.position
newNode.name = self.name
self.removeFromParent()
parent.addChild(newNode)
var corners = UIRectCorner()
if topLeft { corners = corners.union(.bottomLeft) }
if topRight { corners = corners.union(.bottomRight) }
if bottomLeft { corners = corners.union(.topLeft) }
if bottomRight { corners = corners.union(.topRight) }
newNode.path = UIBezierPath(roundedRect: CGRect(x: -(newNode.frame.width/2),y:-(newNode.frame.height/2),width: newNode.frame.width, height: newNode.frame.height),byRoundingCorners: corners, cornerRadii: CGSize(width:radius,height:radius)).cgPath
}
}
而且使用它是這樣的...
aShapeNode.roundCorners(topLeft: true, topRight: true, bottomLeft: false, bottomRight: false, radius: 5,parent: popup)
大聲笑。你今天早些時候回答了我的其他SpriteKit場景問題。我喜歡使用場景編輯器,但這真的會成爲一個PITA,它具有所有小規模的怪癖以及我將要找到的任何其他內容。 – TheValyreanGroup
看看我的編輯可能有幫助 –
我絕對同意它有很多怪癖,但我仍然認爲它有潛力。現在我正嘗試創建一個完全在場景編輯器中放置的遊戲,但有些東西令人沮喪,但總體來說,它比在代碼中放置,運行,更改一些值,重新運行它快得多,重複,重複,重複 –