-1

I just started to learn Swift and I would like to code the game BubbleBreaker which I have already created in both Java and C# some years ago.

For this, I wanted to create a two-dimensional array of Bubble (which is derived from SKSpriteNode), however, when I am trying to populate the array, I always get an "index out of range error" at index [0][0]. Can somebody please help me?

class GameScene: SKScene {
    //game settings
    private var columns = 10
    private var rows = 16
    private var bubbleWidth = 0
    private var bubbleHeight = 0

    //bubble array
    private var bubbles = [[Bubble]]()

    override func didMove(to view: SKView) {
        initializeGame()
    }

    private func initializeGame() {
        self.anchorPoint = CGPoint(x: 0, y: 0)        

        //Optimize bubble size
        bubbleWidth = Int(self.frame.width) / columns
        bubbleHeight = Int(self.frame.height) / rows

        if bubbleWidth < bubbleHeight {
            bubbleHeight = bubbleWidth
        }
        else {
            bubbleWidth = bubbleHeight
        }

        //Initialize bubble array
        for i in 0 ... columns-1 {
            for j in 0 ... rows-1 {
                let size = CGSize(width: bubbleWidth, height: bubbleHeight)
                let newBubble = Bubble(size: size)
                newBubble.position = CGPoint(x: i*bubbleWidth, y: j*bubbleHeight)

                bubbles[i][j] = newBubble // THIS IS WERE THE CODE BREAKS AT INDEX [0][0]

                self.addChild(newBubble)
            }
        }
    }
}
rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • The answers below point you in the right direction, but the key point is that there is no such thing as a "two-dimensional array" in Swift's stdlib. `[[Bubble]]` is an array of arrays. That's not the same thing (each "row" can be a different length). If you need a real two-dimensional array (a matrix), then you'll need a specialized type for that. See https://stackoverflow.com/a/53421491/97337 for an example of a Matrix type. – Rob Napier Dec 04 '18 at 18:34

2 Answers2

0

bubbles starts off empty. There's nothing at any index.

Update your loop to something like this:

//Initialize bubble array
for i in 0 ..< columns {
    var innerArray = [Bubble]()
    for j in 0 ..< rows {
        let size = CGSize(width: bubbleWidth, height: bubbleHeight)
        let newBubble = Bubble(size: size)
        newBubble.position = CGPoint(x: i*bubbleWidth, y: j*bubbleHeight)

        innertArray.append(newBubble)
        self.addChild(newBubble)
    }
    bubbles.append(innerArray)
}

This builds up an array of arrays.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
0

Instead of assigning new value as unexisting value, append new empty array of Bubble for every column and then append to this array newBubble for every row

for i in 0 ... columns-1 {

    bubbles.append([Bubble]())

    for j in 0 ... rows-1 {

        let size = CGSize(width: bubbleWidth, height: bubbleHeight)
        let newBubble = Bubble(size: size)
        newBubble.position = CGPoint(x: i*bubbleWidth, y: j*bubbleHeight)

        bubbles[i].append(newBubble)

        self.addChild(newBubble)
    }
}
Robert Dresler
  • 10,580
  • 2
  • 22
  • 40