2016-04-27 65 views
0

我有一個任意形狀的非自相交多邊形SKShapeNode A.作爲圖形效果,我想在A的頂部放置另一個比例爲0.7的SKShapeNode B.將SKShapeNodes放置在彼此之上以獲得圖形效果

我迷失在Sprite-Kits座標系統中。我無法讓SKShapeNode B正確定位。這應該是一個簡單的問題,但我堅持下去。所有幫助愉快!

    createdShape.name = "whiteArea" 
        createdShape.strokeColor = shapeBorderColor 
        createdShape.lineWidth = 0.0 
        createdShape.fillColor = shapeFillColor 
        createdShape.alpha = 1.0 
        createdShape.zPosition = shapeZPosition 
        createdShape.glowWidth = 3.0 

        let graphicAddonShape = createdShape.copy() as! SKShapeNode 
        graphicAddonShape.setScale(0.7) 
        graphicAddonShape.lineWidth = 0 
        graphicAddonShape.zPosition = shapeZPosition + 1 
        graphicAddonShape.fillColor = UIColor(red: 15/255, green: 15/255, blue: 15/255, alpha: 1.0) 
        graphicAddonShape.name = "blackArea" 
        graphicAddonShape.physicsBody = nil 

        graphicAddonShape.position.x = createdShape.frame.width/2 - graphicAddonShape.frame.width/2 
        graphicAddonShape.position.y = createdShape.frame.height/2 - graphicAddonShape.frame.height/2 
        createdShape.addChild(graphicAddonShape) 
        self.addChild(createdShape) 
+0

如果你能提供一個[最小,完整和可驗證的例子](http://stackoverflow.com/help/mcve)代碼,可能會導致你正在談論的問題,這將會簡單得多。此外,作爲一個補充,你應該添加一張圖片來說明所需的輸出。 – Whirlwind

+0

除了@SteveIves已經指出的內容之外,還有一些需要注意的事情就是'frame'屬性忽略了該節點的子節點。如果你想讓孩子參與計算,你應該使用'calculateAccumulatedFrame()'方法。 – Whirlwind

回答

0

下面是使用你的代碼的完整的Xcode操場加上我已經在上面添加了一些代碼來創建現場,並設置了行程一些變量和填充顏色等

//: Playground - noun: a place where people can play 

import UIKit 
import SpriteKit 
import XCPlayground 

let sceneView = SKView(frame: CGRect(x: 0, y: 0, width: 480, height: 320)) 

let scene = SKScene(size: CGSize(width: 480, height: 320)) 

sceneView.presentScene(scene) 

XCPShowView("My Scene", view: sceneView) 

var createdShape = SKShapeNode(circleOfRadius: 35) 
let shapeBorderColor = SKColor.greenColor() 
let shapeFillColor = SKColor.yellowColor() 
let shapeZPosition: CGFloat = 1.0 
scene.backgroundColor = SKColor.grayColor() 

createdShape.name = "whiteArea" 
createdShape.strokeColor = shapeBorderColor 
createdShape.lineWidth = 0.0 
createdShape.fillColor = shapeFillColor 
createdShape.alpha = 1.0 
createdShape.zPosition = shapeZPosition 
createdShape.glowWidth = 3.0 

createdShape.position = CGPoint(x: 150, y: 150) 

let graphicAddonShape = createdShape.copy() as! SKShapeNode 
graphicAddonShape.setScale(0.7) 
graphicAddonShape.lineWidth = 0 
graphicAddonShape.zPosition = shapeZPosition + 1 
graphicAddonShape.fillColor = UIColor(red: 15/255, green: 15/255, blue: 15/255, alpha: 1.0) 
graphicAddonShape.name = "blackArea" 

graphicAddonShape.position = CGPointZero // This will be in it's parent co-ordinate system 
createdShape.addChild(graphicAddonShape) 
scene.addChild(createdShape) 

要查看輸出,您必須轉到查看 - >助理編輯器 - >顯示助理編輯器。

您會看到我將graphicAddonShape的position設置爲CGPoint(0,0)。 SKShapeNode的position屬性指的是它的中心,所以我們把graphicAddonShape的中心放在其父母的(createdShape)座標系統的位置(0,0)處,即createdShape的中心。要看到差距,第二從最後一行從改變:

createdShape.addChild(graphicAddonShape) 

scene.addChild(graphicAddonShape) 

看到黑圈是如何取決於誰它的父是定位。

+0

SKShapeNode中沒有大小屬性​​。你是指框架? – user594883

+0

這似乎並不奏效。結果如下。節點A用白色表示,節點B用黑色表示。節點不排隊,節點之間的差異正在增長,取決於他們靠近的邊界。 http://d.pr/i/119rF – user594883

+0

我重構了包含代碼的問題。 – user594883