【问题标题】:How to rotate SKLabelNode in company with its parent SKShapeNode?如何与其父 SKShapeNode 一起旋转 SKLabelNode?
【发布时间】: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


【解决方案1】:

您需要将摩擦力设置为 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>) 将形状转换为精灵:) let sprite = SKSpriteNode(texture: texture)

【讨论】:

  • 如果有很多不同的纹理你会怎么做? :) 不要忘记地图集。
  • @Whirlwind 我从来没有任何资产可以使用......所以......:P我总是在我的游戏中使用形状哈哈
  • 真的可以从您的 cmets 学到更多。实际上,这些球有超过 4 种不同的颜色。我曾尝试创建具有不同“球背景”纹理的 SKSpriteNode,并添加 SkLabelNode 作为其子级,球正常显示,但标签不规律地不显示其文本,有时在薮球中,有时在全部,有时没有。 :-(
  • @jdleung 很有趣...也许是该代码的新问题?
  • @jdleung 我想知道这是否像.zPosition 问题一样简单?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-07
  • 1970-01-01
相关资源
最近更新 更多