我們必須在應用旋轉之前明確居中SKShapeNode。
import SpriteKit
import PlaygroundSupport
let bounds = CGRect(x: 0, y: 0, width: 400, height: 200)
let skview = SKView(frame: bounds)
PlaygroundPage.current.liveView = skview
在以下的第一和第二實施例中,這是通過設置x
和y
參數而初始化矩形,然後設定位置屬性來完成。
let first = SKShapeNode(rect: CGRect(x:-20, y:-20, width: 40, height: 40))
first.position = CGPoint(x: 70, y: 30)
let second = SKShapeNode(rect: CGRect(x:-20, y:-20, width: 40, height: 40))
second.position = CGPoint(x: 70, y: 30)
second.zRotation = CGFloat.pi * 0.15
second.strokeColor = .red
在下面的第三示例中,這是通過設置centered
到true
而初始化的形狀,然後設定位置屬性來完成。
let path = CGMutablePath();
path.move(to: CGPoint(x: 50,y: 10))
path.addLine(to: CGPoint(x: 90, y: 10))
path.addLine(to: CGPoint(x: 90, y: 50))
path.addLine(to: CGPoint(x: 50, y: 50))
path.addLine(to: CGPoint(x: 50, y: 10))
let third = SKShapeNode(path: path, centered: true);
third.position = CGPoint(x: 70,y: 30);
third.zRotation = CGFloat.pi * 0.35
third.strokeColor = .yellow
let scene = SKScene(size: CGSize(width: 400, height: 200))
scene.scaleMode = SKSceneScaleMode.aspectFill
scene.size = skview.bounds.size
scene.addChild(first);
scene.addChild(second);
scene.addChild(third);
skview.presentScene(scene);
糾正了這個問題。 – 2014-10-10 03:46:04