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



有没有办法用纹理画一个圆形的SKSpriteNode?或者,绘制一个带有纹理的SKShapeNode?

要考虑所有图像都是矩形/正方形。我们看到的圆形,就是我们如何显示彩色像素:圆圈外的,透明的,里面的,是彩色的部分。

  1. 我们需要一个圆圈
  2. 我们给它纹理
  3. 我们将其添加到场景中
  4. 我们从刚刚添加到场景中的这个形状节点中取回一个新纹理
  5. 我们使用这种新纹理制作一个新的精灵
  6. 我们将精灵添加到场景中

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

最新更新