我在这样的类中定义了一个 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/