swift - 无法将类型 'Array<SKtexture>' 的值分配给类型 'SKTexture'

标签 swift xcode sprite-kit

我在这样的类中定义了一个 SKTexture 数组:

var walking: Array<SKTexture> = []

然后我在同一个类中有一个函数返回该数组的一个元素:

func running() -> SKTexture{
    self.counter_run += self.counter_run
    if (self.counter_run >= 2){
        self.counter_run = 0
    }
    return self.walking[counter_run]
}

我在代码的另一部分使用该函数来为我的角色设置动画:

override func update(_ currentTime: TimeInterval) {
    player.texture = koopa.walking()
}

但是我得到这个错误:

Cannot assign value of type 'Array<SKtexture>' to type 'SKTexture'

最佳答案

您正在尝试将数组 walking 分配给类型为 SKTexture 的节点纹理。

根据您想要实现的目标,为节点设置一个 walking 纹理:

player.texture = walking[counter_run]

或使用一个 Action :

player.runAction(SKAction.animateWithTextures(walking, timePerFrame: 0.5))

编辑: 您的方法的纹理动画取决于您很可能不想要的帧速率。因此我建议使用后者。

关于swift - 无法将类型 'Array<SKtexture>' 的值分配给类型 'SKTexture',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41784648/

相关文章:

ios - 图像名称作为变量(Swift)

ios - iOS 中用于转换的 UITextField 更改

ios - SFSafariViewController : how to provide custom activities?

iphone - iOS中日志文本中针对项目名称的编号

ios - 类型 '[AnyObject!' 无法隐式向下转换为 'PFObject'

ios - SKShapeNode 或其他东西上的 anchor

ios - 完成 block 方法与DispatchQueue

ios - 在 Xcode 的主视图中添加两个 TableView

ios - 哪个 Sprite Kit 节点定义了 shouldRasterize 属性?

swift - 如何更改另一个类的按钮图像?