swift - 画一个带有纹理的圆形SKSpriteNode?

标签 swift sprite-kit

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

最佳答案

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

  1. 我们需要一个圆
  2. 我们对其进行纹理处理
  3. 我们将其添加到场景中
  4. 我们从刚刚添加到场景中的形状节点取回新纹理
  5. 我们使用这个新纹理制作一个新的 Sprite
  6. 我们将 Sprite 添加到场景

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

关于swift - 画一个带有纹理的圆形SKSpriteNode?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45783547/

相关文章:

ios - swift/iOS : Tap Gesture to find which text field just became the first responder

ios - 如何在 Swift 中解析来自 Alamofire API 的 JSON 响应?

swift - 如何在不出现性能问题的情况下暂停和停止 SKScene?

ios - 单击提要中的个人资料 - swift

ios - Swift 不平衡调用开始/结束外观转换

swift - 在我的 Sprite-Kit 游戏中实现此操作的正确方法是什么?

ios - 将多个 userdefaults 变量存储在 spritekit 中的某种数组、类或结构中?

ios - swift SpriteKit : Creating a realistic driving experience 2D

ios - Swift 字符串的范围运算符(..< 和 ...)

sprite-kit - 为 SKSpriteNode 添加白色边框