如何使用纹理绘制一个圆形的SKSpriteNode?

3
有没有一种方法可以绘制带有纹理的圆形SKSpriteNode?或者,用纹理绘制SKShapeNode的方法呢?

1
你能把纹理做成圆角的吗? - Sweeper
纹理实际上是由用户现场选择的,因此它们不能是圆形的。 - Lim Thye Chean
“带纹理”是什么意思?有几种方法可以创建圆形精灵。 - 0x141E
1个回答

3

需要考虑的是所有的图片都是矩形/正方形。我们所看到的圆形,实际上只是显示彩色像素的方法:圆形外部是透明的,内部是彩色的部分。

  1. we need a circle
  2. we texture it
  3. we add it to the scene
  4. we take back a new texture from this shape node that we just added to the scene
  5. we make a new sprite using this new texture
  6. we add the sprite to the scene

    let shape: SKShapeNode = SKShapeNode(circleOfRadius: 50)
    shape.name = "shape"
    shape.lineWidth = 1 // this way we see that this is a circle
    shape.fillColor = .white // to see the true colors of our image
    //shape.strokeColor = .white
    //shape.glowWidth = 0
    shape.fillTexture = SKTexture(imageNamed:"myImage")
    shape.position = CGPoint(x: 0, y:200)
    self.addChild(shape)
    
    let newTexture: SKTexture = (self.view?.texture(from: self.childNode(withName: "shape")!))!
    let sprite: SKSpriteNode = SKSpriteNode(texture: newTexture)
    self.addChild(sprite) 
    
    shape.removeFromParent()
    

1
您可以通过 view?.texture(from: shape) 来创建一个纹理,而不必将其添加到场景中。 - 0x141E

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接