如何將SKLabelNode與其父SKShapeNode一起旋轉?
當長按屏幕,用下面的代碼創建球:
var n = 1
func addBall(_ x:CGFloat){
let numShape = SKShapeNode(circleOfRadius: 30)
numShape.name = "ball"
numShape.position = CGPoint(x: x, y: frame.maxY-40)
numShape.physicsBody = SKPhysicsBody(circleOfRadius: 30)
numShape.fillColor = SKColor.white
numShape.physicsBody?.density = 0.1
numShape.physicsBody?.affectedByGravity = true
numShape.physicsBody?.friction = 0;
numShape.physicsBody?.restitution = 0.6
numShape.physicsBody?.allowsRotation = true
numShape.physicsBody?.isDynamic = true
let numLabel = SKLabelNode(fontNamed: "Helvetica")
numLabel.text = "\(n)"
numLabel.name = "\(n)"
numLabel.fontColor = .red
numLabel.position = CGPoint(x: 0, y: 0)
numLabel.verticalAlignmentMode = .center
numShape.addChild(numLabel)
self.addChild(numShape)
n += 1
}
的球可以旋轉,但其childNode numlabel並不在公司與它們旋轉。我嘗試更新他們的zRotation,如下所示:
override func update(_ currentTime: TimeInterval) {
self.enumerateChildNodes(withName: "ball") {
node, stop in
if (node is SKShapeNode) {
let ball = node as! SKShapeNode
let num = ball.children[0]
num.zRotation = ball.zRotation
}
}
}
他們仍然拒絕旋轉。如果我直接像num.zRotation = 2
那樣更改zRotation,它們就可以工作。
如何讓他們與SKShapeNode一起旋轉?
Thx。
在哪裏:自己只有2圖像只用幾行重構的平你讓球旋轉?你可以顯示這個代碼嗎? – ColdSteel
@ColdSteel我已經發布了代碼。我在'override func update(_ currentTime:TimeInterval)' – jdleung
'num.zRotation = ball.zRotation'中更新了他們的zRotation,但是什麼改變了球zRotation? 我建議你在更新方法內記錄ball.zRotation並告訴我它是否更改。附:你不需要將num的Z旋轉與球同步,而num是球的子球,它將隨着球旋轉 – ColdSteel