Swift 多维纹理数组
Swift multidimensional array of textures
我正在尝试将一组动画纹理从纹理图集加载到数组中,但在尝试将帧附加到数组时出现错误。
基本上我想要一个多维纹理数组,其中每一行代表一个动画,它是一个帧数组。像这样:
[[FirstFrameTexture, SecondFrameTexture, ThirdFrameTexture...], //first animation
[FirstFrameTexture, SecondFrameTexture, ThirdFrameTexture...], //second animation
[FirstFrameTexture, SecondFrameTexture, ThirdFrameTexture...]] //third animation
我像这样初始化了一个数组:
var animations = [[SKTexture]]()
但是当我尝试像这样将纹理附加到数组中时
animations[0].append(SKTexture(imageNamed: "test"))
我在这一行中收到一条错误消息:
fatal error: Cannot index empty buffer
关于如何解决这个问题的任何想法?
当您尝试向其附加动画时,索引 0 处的第一个数组尚未创建。
首先添加一个嵌入数组的动画,例如:
animations.append([SKTexture(imageNamed: "test")])
然后就可以在数组中添加动画了:
animations[0].append(SKTexture(imageNamed: "test2"))
我正在尝试将一组动画纹理从纹理图集加载到数组中,但在尝试将帧附加到数组时出现错误。
基本上我想要一个多维纹理数组,其中每一行代表一个动画,它是一个帧数组。像这样:
[[FirstFrameTexture, SecondFrameTexture, ThirdFrameTexture...], //first animation
[FirstFrameTexture, SecondFrameTexture, ThirdFrameTexture...], //second animation
[FirstFrameTexture, SecondFrameTexture, ThirdFrameTexture...]] //third animation
我像这样初始化了一个数组:
var animations = [[SKTexture]]()
但是当我尝试像这样将纹理附加到数组中时
animations[0].append(SKTexture(imageNamed: "test"))
我在这一行中收到一条错误消息:
fatal error: Cannot index empty buffer
关于如何解决这个问题的任何想法?
当您尝试向其附加动画时,索引 0 处的第一个数组尚未创建。
首先添加一个嵌入数组的动画,例如:
animations.append([SKTexture(imageNamed: "test")])
然后就可以在数组中添加动画了:
animations[0].append(SKTexture(imageNamed: "test2"))