gpt4 book ai didi

ios - 如何与其父 SKShapeNode 一起旋转 SKLabelNode?

转载 作者:搜寻专家 更新时间:2023-10-31 22:56:49 26 4
gpt4 key购买 nike

enter image description here

当按下屏幕时,使用以下代码创建的球:

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 个形状底部的绘制计数:

enter image description here

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)
}
}

enter image description here


这里的魔法是使用 let texture = SKView().texture(from: <SKNode>)将形状转换为 Sprite :) let sprite = SKSpriteNode(texture: texture)

关于ios - 如何与其父 SKShapeNode 一起旋转 SKLabelNode?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44985018/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com