Once the model was viewable it was time to load levels from a file in order to enable dynamic patterns. The file needed to have a pattern defined so that every level isn’t just a 9×9 grid. This pattern would be best represented in an array of arrays or a 2D Array. That would most easily be stored in a JSON file.
Although we defined a fruit layer we needed to define a slot layer where a 9×9 grid of slots would define whether a grid position would even be able to contain a fruit. This way we would have a way to not put a fruit into [3,2] if tiles[3,2] is nil.
To test we’ll place the JSON file in the mainBundle and implement an extension to Dictionary to facilitate loading in the JSON while instantiating the level. This is an ideal usage of Swift’s extension mechanism.
By checking whether we should put a fruit in when we create the initial Fruits in the Level class we now have the pattern of fruit that we defined in the JSON file for the tiles 2D Array in order to make Levels.
By introducing a tileLayer in the Game Scene we can show the tile image behind the fruit image by adding the tile image much like we add the fruit image. Running the current code brings up the 9×9 grid of fruit with the tile structure.
Tomorrow, I’ll start to add gesture recognizers to enable swapping fruits in the grid.