将SKShapeNodes定位在彼此之上以获得图形效果

时间:2016-04-27 13:47:42

标签: ios swift sprite-kit skshapenode

我有一个任意形状的非自相交多边形SKShapeNode A.作为一个图形效果,我想在A的顶部放置另一个尺度为0.7的SKShapeNode B.

我在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)

1 个答案:

答案 0 :(得分: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)

要查看输出,您必须转到View-> Assistant Editor-> Show Assistane Editor。

您会看到我将graphicAddonShape的position设置为CGPoint(0,0)。 SKShapeNode的position属性引用它的中心,因此我们将graphicAddonShape的中心放在它的父母的位置(0,0)上。 s(createdShape)协调系统即createdShape的中心。要查看差异,请从以下位置更改第2行:

createdShape.addChild(graphicAddonShape) 

scene.addChild(graphicAddonShape)

了解黑圈是如何定位的,取决于它的父母是谁。