Gamescene spriteNodes在数组中

时间:2018-04-05 20:03:58

标签: swift sprite-kit skspritenode

在这片代码上获取错误。 我不想手动(在代码中)通过GameScene.sks上的spriteNode - for循环必须以某种方式工作?任何想法......

let sprites:[SKSpriteNode] = [block1, block2, block3, block4,
    block5, block6, block7, block8, block9, block10, block11,
    block12, block13, block14, block15, block16, block17,
    block18, block19, block20, block21, block22, block23,
    block24, block25, block26, block27, block28, block29, 
    block30, block31, block32, block33, block34, block35,
    block36, block37, block38, block39, block40, block41,
    block42, block43, block44, block45, block46, block47,
    block48, block49, block50, block51, block52, block53,
    block54, block55, block56, block57, block58, block59,
    block60, block61, block62, block63, block64, block65,
    block66, block67, block68, block69, block70, block71,
    block72, block73, block74, block75, block76, block77,
    block78, block79, block80, block81, block82, block83, block84]

for sprite in sprites {
    self.sprite; in sprites = (self.childNode(withName: sprite.name) as? SKSpriteNode)!
    sprite.name = "Green"
    sprite.zPosition = 3
}

1 个答案:

答案 0 :(得分:0)

我猜你的.sks文件中有一堆SKSpriteNodes并且你想在代码中循环它们来设置它们,这是你的非描述性描述吗?

这样的事情对你来说可能会更好,如果那确实是你想要的。

let spriteNames: [String] = ["block1", "block2", "block3"]
let sprites = [SKSpriteNode]()

for spriteName in spriteNames {
    if let sprite = self.childNode(withName: spriteName) as? SKSpriteNode {
        sprite.name = "Green"
        sprite.zPosition = 3

        sprites.append(sprite)
    }
}