What good would a shop be if the player couldn’t access it?
Switch to the GameScene.swift file and add the following new properties (be sure to add them below the line that reads var isContinue: Bool = false):
| // Reference Scene |
| let shopButton = SKSpriteNode(imageNamed: "shop") |
| var shopScene: ShopScene! |
| var shopIsOpen = false |
You’ll use these properties to show and hide the shop scene.
Jump to the end of the file, and inside the GameScene extension, below the useContinue() method, add the following code:
| func setupShop() { |
| // Set up Shop Button |
| shopButton.name = "shop" |
| shopButton.zPosition = Layer.shop.rawValue |
| shopButton.position = CGPoint(x: frame.minX + 75, y: viewBottom() + 75) |
| addChild(shopButton) |
| |
| // Set up Shop Scene and add it to the Game Scene |
| shopScene = ShopScene(in: self) |
| shopScene.zPosition = Layer.shop.rawValue |
| shopScene.position = CGPoint(x: frame.midX, y: frame.midY) |
| addChild(shopScene!) |
| |
| // Set up notification observers and Shop UI |
| shopScene.setupObservers() |
| shopScene.setupShop() |
| } |
This method adds the shop scene as a reference node. You first learned about reference nodes in Use Reference Nodes in Your Scene. It also calls the methods responsible for setting up the shop observers and products.
Next, in the didMove(to:) method (near the top of the file), below the setupContinues() method, add a call to the method you just created, like so:
| setupShop() |
Finally, jump to the touchDown(atPoint:) method and update the code from this:
| func touchDown(atPoint pos: CGPoint) { |
| let touchedNodes = nodes(at: pos) |
| for touchedNode in touchedNodes { |
| // print("touchedNode: (String(describing: touchedNode.name))") |
| if touchedNode.name == "player" && gameInProgress == true { |
| movingPlayer = true |
| } else if touchedNode == watchAdButton && gameInProgress == false { |
| gameSceneDelegate?.showRewardVideo() |
| return |
| } else if touchedNode == continueGameButton && gameInProgress == false { |
| useContinue() |
| return |
| } else if touchedNode == startGameButton && gameInProgress == false { |
| spawnMultipleGloops() |
| return |
| } |
| } |
| } |
to this (the highlighted code shows the changes):
| func touchDown(atPoint pos: CGPoint) { |
| let touchedNodes = nodes(at: pos) |
| for touchedNode in touchedNodes { |
| // print("touchedNode: (String(describing: touchedNode.name))") |
» | if shopIsOpen == true { |
» | if touchedNode.name == "buy" { |
» | shopScene.purchaseProduct(node: touchedNode) |
» | return |
» | } else if touchedNode.name == "shop.restore" { |
» | shopScene.restorePurchases() |
» | return |
» | } else if touchedNode.name == "shop.exit" { |
» | shopScene.endInteraction() |
» | shopIsOpen = false |
» | return |
» | } |
» | } else if shopIsOpen == false { |
» | if touchedNode.name == "shop" && gameInProgress == false { |
» | shopScene.beginInteraction() |
» | shopIsOpen = true |
» | return |
| } else if touchedNode.name == "player" && gameInProgress == true { |
| movingPlayer = true |
| } else if touchedNode == watchAdButton && gameInProgress == false { |
| gameSceneDelegate?.showRewardVideo() |
| return |
| } else if touchedNode == continueGameButton && gameInProgress == false { |
| useContinue() |
| return |
| } else if touchedNode == startGameButton && gameInProgress == false { |
| spawnMultipleGloops() |
| return |
| } |
| } |
| } |
| } |
The new code adds some additional checks: when the player taps on the Shop button in the bottom-left corner of the screen, the shop scene will open. Likewise, when the player taps the shop scene’s Exit button, the shop will close. This new code also handles when the player taps the shop’s Buy and Restore Purchases buttons.
Build and run the project to test opening and closing the shop. (You won’t see any products yet, but don’t worry, you’ll take care of that next.)
Notice how the shop scene animates into and out of the game scene when you tap the Shop button and Exit button.
Now that you have the shop scene set up and added as a working reference node, you’re ready to get into the nitty-gritty of StoreKit.
Wait, hold on a minute. StoreKit? What the heck is that?