【问题标题】:Swift SKSpriteNode with UIImage带有 UIImage 的 Swift SKSpriteNode
【发布时间】:2015-08-09 16:44:16
【问题描述】:

我在使用 UIImage 创建 SKSpriteNode 时遇到问题。 我正在使用 PaintCode 创建用于我的应用程序的资产(PaintCode 创建一个 Swift 文件,其中包含允许我显示创建的图像的绘图方法和图像方法)。

我现在的问题是我想使用我的 PaintCode 图像创建一个简单的 SKSpriteNode,但我不知道如何将它们添加到 SpriteNode。

我尝试过这样的事情(第 18 行):

let Bottom = SKSpriteNode(texture: SKTexture(image: StyleKitName.drawBottom(frame: CGRect(x: 50, y: 50, width: 50, height: 50), isWinter: false)))

但它返回以下错误:

/Users/myMacName/Documents/App Development/AppName/AppName/GlobalFunctions.swift:18:44: Cannot find an initializer for type 'SKTexture' that accepts an argument list of type '(image: ())'

StyleKitName.drawBottom 函数返回一个 UIImage。 如果有人能告诉我如何使用 UIImage 创建 SKSpriteNode,那就太好了。

感谢您的帮助。谢谢。

【问题讨论】:

  • 你在哪里使用let Bottom = SKSpriteNode(texture: SKTexture(image: StyleKitName.drawBottom(frame: CGRect(x: 50, y: 50, width: 50, height: 50), isWinter: false)))这行代码?
  • 这行代码是第18行,同一行出现错误。

标签: ios swift uiimage skspritenode paintcode


【解决方案1】:

如果你有一个 UIImage,你同样可以用它制作一个精灵节点:

let Image = //do your setup here to make a UIImage
let Texture = SKTexture(image: Image)
let Sprite = SKSpriteNode(texture:Texture)

我不确定您在哪里或如何调用这些方法,但我可以保证这些是正确的初始化程序。

【讨论】:

  • 感谢这帮助我意识到我调用了错误的函数(“drawButton”而不是由 PaintCode 创建的“imageOfButton”)。很抱歉因为这么愚蠢的错误浪费了你的时间。
  • 很高兴你知道了!
  • "small" 升级到问题 - 对于带有 gif 的扩展 UIImage 也一样?这是一个很大的问题,因为图像必须是异步的,所以在这个答案中SKTexture 总是会出错.. 我知道没有人要求它,我很抱歉.. 仍然很高兴得到答案:)
【解决方案2】:

如果你想从场景编辑器连接它

    let movieImage = self.childNode(withName: "MovieImage") as! SKSpriteNode?
    let Image = UIImage(named: "imageName")
    let Texture = SKTexture(image: Image!)
    movieImage?.texture = Texture

【讨论】:

    【解决方案3】:

    如果你有一台 iPad 并且想将 UIImage 分配给 SKSpriteNode

    let myImage = SKSpriteNode(texture:SKTexture(image: UIImage))
    

    现在您可以从 iPad 上的文件应用程序中导入图像并轻松为其分配图像。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-19
      • 1970-01-01
      • 2014-10-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多