从数组生成网格

时间:2017-02-11 10:04:25

标签: arrays swift for-loop grid

我有这个块数组:

let blocks = [Blocks]()

我在该数组中添加了16个块。以下是我如何生成4x4网格:

for i in 0...3 {
            for j in 0...3 {
                for b in blocks {
                    b.position = CGPoint(x: CGFloat(i)*b.frame.size.width + xOffset, y: yPos)
                    addChild(b)  <<-- Error here
                    yPos = yPos - b.frame.size.height - yOffset

                }
            }

我收到此错误:

Attemped to add a SKNode which already has a parent

我该怎么办?

1 个答案:

答案 0 :(得分:1)

如果替换

行,它应该有效
for b in blocks

let b = blocks[i*4 + j]

您正在使用当前代码

添加所有块16次
相关问题