2017-03-02 29 views
0

圓角矩形我有下面的代碼舍入從現場建設者已經存在的矩形...與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) 

回答

3

不是您要想聽,但是那是因爲你不能設置在場景編輯器中SKShapeNode的寬度(要我知識)。爲了讓ShapeNode具有500的寬度,你必須調整xScale。當您調整xScale時,xScale會重新應用到路徑上(指數級增長)。如果您在代碼中創建SKShapeNode是沒有問題的調整圓角

let round = SKShapeNode(rect: CGRect(x: 0, y: -150, width: 500, height: 200)) 
    round.fillColor = .red 
    addChild(round) 
    print(round.frame.width) 
    round.path = UIBezierPath(roundedRect: CGRect(x: -250, y: -100, width: 500, height: 200), cornerRadius: 50).cgPath 
    print(round.frame.width) 

編輯

如果你有你的心臟上使用您的場景編輯器可以將您的ShapeNode和伸展它設置到你想去的地方,那麼你可能只是在做一個小代碼轉換來獲取你想要

if let round = self.childNode(withName: "biground") as? SKShapeNode { 

     let biground = SKShapeNode(rect: round.frame) 
     biground.fillColor = round.fillColor 
     biground.position = round.position 
     addChild(biground) 
     round.removeFromParent() 

     print(biground.frame.width) 
     biground.path = UIBezierPath(roundedRect: CGRect(x: -250, y: -100, width: 500, height: 200), cornerRadius: 50).cgPath 
     print(biground.frame.width) 
    } 

這個結果只是重新創建在C形ODE根據您在場景編輯器概述了與輪邊緣完美

編輯2

我一直的印象是SKShapeNodes真的是低效的(我敢肯定,他們下泄露記憶)。所以我總是設置我的圓形矩形。

let outsideTexture = SKTexture(imageNamed: "round_square_white") 
    let outside = SKSpriteNode(texture: outsideTexture)  
    let insetX: CGFloat = 20 
    let insetY: CGFloat = 20 
    let cellSize = CGSize(width: 500.0, height: 500.0) 

    outside.centerRect = CGRect(x: CGFloat(insetX/outside.size.width), y: CGFloat(insetY/outside.size.height), width: CGFloat((outside.size.width - insetX * 2)/outside.size.width), height: CGFloat((outside.size.height - insetY * 2)/outside.size.height)) 
    //outside.position = CGPointMake(gameModel.gameWidth/2, (outside.size.height)/2); 
    outside.xScale = cellSize.width/outside.size.width 
    outside.yScale = cellSize.height/outside.size.height 
    outside.zPosition = 10 
    outside.position = CGPoint(x: CGFloat(0), y: CGFloat(-0.35 * self.size.height/2)) 
    self.addChild(outside) 

值得注意的是,勾畫出一個圓角正方形/長方形完美但類似於從你必須把一個空的細胞在這個孩子添加到場景編輯器中的尺度問題,否則他們擴展到圓角正方形。

+0

大聲笑。你今天早些時候回答了我的其他SpriteKit場景問題。我喜歡使用場景編輯器,但這真的會成爲一個PITA,它具有所有小規模的怪癖以及我將要找到的任何其他內容。 – TheValyreanGroup

+0

看看我的編輯可能有幫助 –

+0

我絕對同意它有很多怪癖,但我仍然認爲它有潛力。現在我正嘗試創建一個完全在場景編輯器中放置的遊戲,但有些東西令人沮喪,但總體來說,它比在代碼中放置,運行,更改一些值,重新運行它快得多,重複,重複,重複 –