【发布时间】:2017-07-08 10:11:31
【问题描述】:
当按下屏幕时,使用以下代码创建的球:
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 一起旋转?
谢谢。
【问题讨论】:
-
你在哪里让球旋转?请出示此代码好吗?
-
@ColdSteel 我已经发布了代码。我在
override func update(_ currentTime: TimeInterval)中更新了他们的 zRotation -
num.zRotation = ball.zRotation但是球的 zRotation 会发生什么变化?我建议您在 update 方法中记录 ball.zRotation 并告诉我它是否发生了变化。附言您不需要将 num 的 Z 旋转与球同步,而 num 是球的子级,它将与球一起旋转 -
@ColdSteel 开始时,球从屏幕顶部落下,碰撞将其 zRotation 从 -9.xx 更改为 9.xx,但如果不再有碰撞,它的 zRotation 变为0.0xxxxxx。我发现 SKLabelNode 一开始没有旋转,所以我尝试同步它们。 ;-)
-
@ColdSteel 找到了解决方案!我试着编辑了physicsBody的每一个变量,我发现摩擦力一定不能为0。谢谢你的帮助。
标签: ios swift sprite-kit skspritenode skshapenode