0

I'm trying to make a game which spawns nodes that will come down from the top of the screen. I want my programme to choose from a multitude of nodes with the same properties(but they look different). Is there a way of making an array of nodes that I can apply the properties to at the same time, rather than having to go through each one and right it all out over and over. I'm a beginner coder and haven't got any code to show except for the variables that I have already.

    var SY = SKSpriteNode(imageNamed: "slowYellow")
    let SG = SKSpriteNode(imageNamed: "slowGreen")
    let SP = SKSpriteNode(imageNamed: "slowPurple")
    let SR = SKSpriteNode(imageNamed: "slowRed")
    let FY = SKSpriteNode(imageNamed: "fastYellow")
    let FG = SKSpriteNode(imageNamed: "normalGreen")
    let FP = SKSpriteNode(imageNamed: "normalPurple")
    let FR = SKSpriteNode(imageNamed: "fastRed")
    let NY = SKSpriteNode(imageNamed: "normalYellow")
    let NG = SKSpriteNode(imageNamed: "normalGreen")
    let NP = SKSpriteNode(imageNamed: "normalPurple")
    let NR = SKSpriteNode(imageNamed: "normalRed")
    let SB = SKSpriteNode(imageNamed: "slowBlack")
    let NB = SKSpriteNode(imageNamed: "normalBlack")
    let FB = SKSpriteNode(imageNamed: "fastBlack")
Wain
  • 118,658
  • 15
  • 128
  • 151
  • 1
    The best thing to do here is probably to use a designated initialiser like: `initWithColor:` that sets the other properties in it's implementation. – Rob Sanders Jan 22 '16 at 09:10
  • How would I do this? Also the nodes I'm using use images that I've already made in other softwares (.png), can I still use "initWithColor:" – Cockettino Jan 26 '16 at 13:33

1 Answers1

1

This sounds like the ideal candidate for subclassing

Subclassing allows you to setup common functionality for a given superclass (in your case, SKSpriteNode). This will allow you to create multiple objects with the same base functionality.

You can create your own convenience initialiser (it's not technically overriding SKSpriteNode's convenience init) and setup your common properties there. For example:

class MyCustomNode: SKSpriteNode {

    convenience init(imageNamed name:String) {

        let tex = SKTexture(imageNamed: name);
        let nodeSize = CGSizeMake(50, 50);

        self.init(texture: tex, color: UIColor.clearColor(), size: nodeSize)

        // Do extra setup for common functionality here
    }  
}

Then, in order to use it, I recommend you create an array of your image names and create the nodes in a for loop. For example:

let birds :[String] = ["slowYellow", "slowGreen", "slowPurple", "slowRed", "fastYellow", "fastGreen", "fastPurple", "fastRed", "normalYellow", "normalGreen", "normalPurple", "normalRed", "slowBlack", "normalBlack", "fastBlack"]

var birdNodes = [MyCustomNode]()
for bird in birds {
    let node = MyCustomNode(imageNamed: bird)
    node.position = // do position logic
    addChild(node)
    birdNodes.append(node)
}

You can then refer to your bird sprites through the birdNodes array.

You can also go further by overriding more functions to further customise your subclass.

I suggest you have a look at the Swift documentation on inheritance. That will give you a good start on subclassing.

Community
  • 1
  • 1
Hamish
  • 78,605
  • 19
  • 187
  • 280
  • Thanks for that, so can I just add the files to the subclass like this: `convenience init(imageNamed name: ["slowYellow", "slowGreen", "slowPurple", "slowRed", "fastYellow", "fastGreen", "fastPurple", "fastRed", "normalYellow", "normalGreen", "normalPurple", "normalRed", "slowBlack", "normalBlack", "fastBlack"]) {` – Cockettino Jan 26 '16 at 11:22
  • no, you'd want to define your array of image names *outside* of the subclass (as they're unique), and then perhaps use a for loop to create each of your objects, looping through the names in the array and initialising each node with that. – Hamish Jan 26 '16 at 12:01
  • Oh okay, so I've made my array of images now. Sorry to sound like an idiot but which parts of the source code you gave me do I edit to fit my coding? I've called my array "birds". Thanks – Cockettino Jan 26 '16 at 21:41
  • I have added a usage example to my answer – Hamish Jan 26 '16 at 21:50
  • When I put this into my program it tells me that the for loop has an error and "expected declaration", what does this mean? – Cockettino Jan 29 '16 at 08:55
  • @Cockettino add the code to the OP... there's a chance you're just missing a bracket or something – Hamish Jan 29 '16 at 19:48