1
votes

The app I'm working on has multiple nodes which are added repeatedly to the game. What I have been doing is creating new SKSpriteNodes using an array of texture, and once the node has been added, I assign that node with a specific physics body. But what I would like to do is instead of a having an array of textures, have an array of SKSpriteNodes with the physics body pre-assigned.

I believe I have figured out how to do this, the problem I have is when I try to add that SKSpriteNode more then once to the scene.

For example, below is I'm been playing around with, which works, but if I add another line of addChild(arraySegment_Type1[0] as SKSpriteNode), I get an error and the app crashes.

override func didMoveToView(view: SKView) {

    anchorPoint = CGPointMake(0.5, 0.5)

    var arraySegment_Type1:[SKSpriteNode] = []
    var sprite = SKSpriteNode(imageNamed: "myimage")

    sprite.physicsBody = SKPhysicsBody(rectangleOfSize: CGSizeMake(sprite.size.width, sprite.size.height))
    sprite.physicsBody?.dynamic = false
    sprite.name = "mysprite"

    arraySegment_Type1.append(sprite)

    addChild(arraySegment_Type1[0] as SKSpriteNode)

}

Does anybody know how I would be able to load SKSpriteNodes multiple times from an array?

1
you want to load the same SKSpriteNode multiple times into the scene?MaxKargin

1 Answers

5
votes

What your code does at the moment is the following:

  • Creates an empty array of the type [SKSpriteNode]
  • Creates a single SKSpriteNode instance
  • Ads a physicsBody to the sprite
  • Ads this single spriteNode to an array
  • Picks out the first object in the array (index 0) and ads it to the parentNode.

Your app crashes because you are trying to add the same SKSpriteNode instance to the scene several times over.


Here is a reworked version of your code that actually ads several sprites to the scene, it technically works but does not make a lot of sense just yet:

override func didMoveToView(view: SKView) {
    // your ARRAY is not an array-segment it is a collection of sprites
    var sprites:[SKSpriteNode] = []

    let numberOfSprites = 16 // or any other number you'd like

    // Creates 16 instances of a sprite an add them to the sprites array
    for __ in 0..<numberOfSprites {
        var sprite = SKSpriteNode(imageNamed: "myimage")
        sprite.physicsBody = SKPhysicsBody(rectangleOfSize: sprite.size)
        sprite.physicsBody?.dynamic = false
        sprite.name = "mysprite" // not sure what your plans are here...
        sprites.append(sprite)
    }

    // picks out each sprite in the sprites array
    for sprite in sprites {
        addChild(sprite)
    }
}

Now as I said this doesn't make all that much sense. As you've probably noticed the sprites all appear on top of each other for one thing. More importantly, doing this all in the didMoveToView means it makes very little sense going through the whole array-dance, you could simply do the following instead:

override func didMoveToView(view: SKView) {
    let numberOfSprites = 16 // or any other number you'd like

    for __ in 0..<numberOfSprites {
        var sprite = SKSpriteNode(imageNamed: "myimage")
        // skipping the physicsBody stuff for now as it is not part of the question

        addChild(sprite)
    }
}

An improvement obviously, but all sprites are still covering each other, and didMoveToView might not be the best place to handle all this anyway, so perhaps something like this to illustrate the point:

override func didMoveToView(view: SKView) {
    let sprites = spritesCollection(count: 16)
    for sprite in sprites {
        addChild(sprite)
    }
}

private func spritesCollection(#count: Int) -> [SKSpriteNode] {
    var sprites = [SKSpriteNode]()
    for __ in 0..<count {
        var sprite = SKSpriteNode(imageNamed: "myimage")
        // skipping the physicsBody stuff for now as it is not part of the question

        // giving the sprites a random position
        let x = CGFloat(arc4random() % UInt32(size.width))
        let y = CGFloat(arc4random() % UInt32(size.height))
        sprite.position = CGPointMake(x, y)
        sprites.append(sprite)
    }
    return sprites
}

Now, long-wound as this is it is still just a starting point towards a working and more sensible code-structure...