-1

Getting an error on this pice of code. I Don't want to go through the spriteNode on GameScene.sks manually (in code) - the for loop must work somehow? Any ideas...

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
}
Ron Myschuk
  • 6,011
  • 2
  • 20
  • 32
  • What's the error? That info needs to be in your question. And point out the exact line causing the error. – rmaddy Apr 05 '18 at 21:14
  • you need a lot more info for this to decipherable. Are you instantiating all the items in that array as SKSpriteNodes anywhere in code? If not than i'm sure that those should be Strings not objects. Also this line ```self.sprite; in sprites = (self.childNode(withName: sprite.name) as? SKSpriteNode)!``` is gibberish – Ron Myschuk Apr 05 '18 at 21:32
  • You know the scene editor supports setting the properties of multiple sprites at once, yes? – Steve Ives Apr 06 '18 at 09:26

1 Answers1

0

I'm guessing that from your not very descriptive description that you have a bunch of SKSpriteNodes in your .sks file and you want to loop through them in code to setup them up???

something like this might work better for you, if that is indeed what you are looking for.

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)
    }
}
Ron Myschuk
  • 6,011
  • 2
  • 20
  • 32