如何与其父 SKShapeNode 一起旋转 SKLabelNode?
How to rotate SKLabelNode in company with its parent 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
}
球可以旋转,但它们的子节点 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一起旋转?
谢谢。
您需要将摩擦力设置为 0 以外的数字。
此外,关于形状节点性能:
查看50个形状底部的绘制数:
for _ in 1...50 {
let x = arc4random_uniform(UInt32(frame.maxX));
let xx = CGFloat(x) - size.width/4
let y = arc4random_uniform(UInt32(frame.maxY))
let yy = CGFloat(y) - size.width/4
let shape = SKShapeNode(circleOfRadius: 50)
shape.strokeColor = .blue
shape.lineWidth = 1
shape.position = CGPoint(x: xx, y: yy)
addChild(shape)
}
但现在将其与这张仅绘制了 2 次且仅进行了几行重构的图像进行比较:
func addFiftySprites() {
let shape = SKShapeNode(circleOfRadius: 50)
shape.strokeColor = .blue
shape.lineWidth = 1
let texture = SKView().texture(from: shape)
for _ in 1...50 {
let x = arc4random_uniform(UInt32(frame.maxX));
let xx = CGFloat(x) - size.width/4
let y = arc4random_uniform(UInt32(frame.maxY))
let yy = CGFloat(y) - size.width/4
let sprite = SKSpriteNode(texture: texture)
sprite.position = CGPoint(x: xx, y: yy)
addChild(sprite)
}
}
这里的魔法是使用 let texture = SKView().texture(from: <SKNode>)
将形状转换为 sprite :) let sprite = SKSpriteNode(texture: texture)
当按下屏幕时,使用以下代码创建的球:
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
}
球可以旋转,但它们的子节点 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一起旋转?
谢谢。
您需要将摩擦力设置为 0 以外的数字。
此外,关于形状节点性能:
查看50个形状底部的绘制数:
for _ in 1...50 {
let x = arc4random_uniform(UInt32(frame.maxX));
let xx = CGFloat(x) - size.width/4
let y = arc4random_uniform(UInt32(frame.maxY))
let yy = CGFloat(y) - size.width/4
let shape = SKShapeNode(circleOfRadius: 50)
shape.strokeColor = .blue
shape.lineWidth = 1
shape.position = CGPoint(x: xx, y: yy)
addChild(shape)
}
但现在将其与这张仅绘制了 2 次且仅进行了几行重构的图像进行比较:
func addFiftySprites() {
let shape = SKShapeNode(circleOfRadius: 50)
shape.strokeColor = .blue
shape.lineWidth = 1
let texture = SKView().texture(from: shape)
for _ in 1...50 {
let x = arc4random_uniform(UInt32(frame.maxX));
let xx = CGFloat(x) - size.width/4
let y = arc4random_uniform(UInt32(frame.maxY))
let yy = CGFloat(y) - size.width/4
let sprite = SKSpriteNode(texture: texture)
sprite.position = CGPoint(x: xx, y: yy)
addChild(sprite)
}
}
这里的魔法是使用 let texture = SKView().texture(from: <SKNode>)
将形状转换为 sprite :) let sprite = SKSpriteNode(texture: texture)